Get the item doubleclick event of listview

后端 未结 16 1446
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:17

What do I need to do in order to reference the double click event for a listview control?

16条回答
  •  天命终不由人
    2020-12-01 02:52

    Here is how to get the selected object and object matching code for the double clicked listview item in a WPF listview:

    /// 
    /// Get the object from the selected listview item.
    /// 
    /// 
    /// 
    /// 
    private object GetListViewItemObject(ListView LV, object originalSource)
    {
        DependencyObject dep = (DependencyObject)originalSource;
        while ((dep != null) && !(dep.GetType() == typeof(ListViewItem)))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
        if (dep == null)
            return null;
        object obj = (Object)LV.ItemContainerGenerator.ItemFromContainer(dep);
        return obj;
    }
    
    private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        object obj = GetListViewItemObject(lvFiles, e.OriginalSource);
        if (obj.GetType() == typeof(MyObject))
        {
            MyObject MyObject = (MyObject)obj;
            // Add the rest of your logic here.
        }
    }       
    

提交回复
热议问题