• 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
    }

  • 相关阅读:
    K8S实战(十七)| 通过 StorageClass 实现动态卷供应
    K8S实战(十六)| 持久化存储卷
    K8S实战(十五)| 存储卷概念
    K8S实战(十四)| ConfigMap 对象
    K8S实战(十三)| Secret 对象
    K8S实战(十二)| 为 Ingress 以及后端 Nginx 增加证书
    批量删除git 本地分支、远程分支、tag
    React 页面间传值的个人总结
    搭建一个属于自己的webpack config(-)
    HTTP 2 新特性
  • 原文地址:https://www.cnblogs.com/ligl/p/5629802.html
Copyright © 2020-2023  润新知