WPF ListView DoubleClick
<ListView x:Name="TrackListView" MouseDoubleClick="MouseDoubleClick"
SelectionChanged="SelectionChanged" >
ListView中的MouseDoubleClick双击时不触发SelectionChanged的选中事件,获取不到当前双击的Item,即this.TrackListView.SelectedItem中的数据对象为null。
protected void MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var item = this.TrackListView.SelectedItem as Track;//is null
//this.TrackListView.SelectedItem is null
}
如果双击时刚好触发了SelectionChanged事件,那么当前listView中的selectItem是有数据的,那怎么解决不触发时而又想获取到当前双击Item数据对象呢,方法如下:
<UserControl.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
</Style>
</UserControl.Resources>
<ListView Name="TrackListView" ItemContainerStyle="{StaticResource itemstyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="Title" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Artist" Width="100" HeaderTemplate="{StaticResource BlueHeader}" DisplayMemberBinding="{Binding Album.Artist.Name}" />
</GridView>
</ListView.View>
</ListView>
protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
var track = ((ListViewItem) sender).Content as Track; //Casting back to the binded Track
}