我想在ListView
事件的MouseEnter
a中检索行单元格的值。使用ListView
生成Binding
项。
如何在不检查SelectedItem
?
更新
XAML
<StackPanel>
<ListView x:Name="listView" MouseEnter="listView_MouseEnter">
<ListView.View>
<GridView>
<GridViewColumn Header="Items"/>
</GridView>
</ListView.View>
</ListView>
<StatusBar VerticalAlignment="Center">
<StatusBarItem>The value is:</StatusBarItem>
<StatusBarItem x:Name="retrievedText" />
</StatusBar>
</StackPanel>
CS
public MainWindow()
{
InitializeComponent();
int[] itemSource = { 1, 12, 3, 44, 12, 45, 6, 8, 9 };
listView.ItemsSource = itemSource;
}
private void listView_MouseEnter(object sender, MouseEventArgs e)
{
string hoveredItem = null; //This will be the retrieved value
retrievedText.Content = hoveredItem;
}
答案 0 :(得分:1)
我们可以覆盖ItemContainerStyle,让项目有机会处理事件:
XAML:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem} }">
<EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
CS:
private void ListBoxItem_MouseEnter(object s, MouseEventArgs e)
{
var item = e.OriginalSource as ListBoxItem;
// depends on what we have put in the content. e.g. string.
var content = item.Content as string;
}
在你的情况下:
XAML:
<StackPanel>
<ListView x:Name="listView">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Items"/>
</GridView>
</ListView.View>
</ListView>
<StatusBar VerticalAlignment="Center">
<StatusBarItem>The value is:</StatusBarItem>
<StatusBarItem x:Name="retrievedText" />
</StatusBar>
</StackPanel>
CS:
private void ListBoxItem_MouseEnter(object s, MouseEventArgs e)
{
var item = e.OriginalSource as ListBoxItem;
retrievedText.Content = item.Content;
}