获取ListView中项的双击事件

40

我需要做什么才能参考一个列表视图控件的双击事件?


你可能想要绑定到项目的双击事件? - TheVillageIdiot
16个回答

64
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
    </Style>
</ListView.ItemContainerStyle>

那么唯一的困难就是如果你对列表视图项映射到的底层对象感兴趣。

private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    object obj = item.Content;
}

这对于我在.NET 3.5 (VS 2008)上没有起作用。然而,PreviewMouseDoubleClick起作用了。这与事件冒泡有关。 - NickV
1
这在 .net 3.5 对我有效,但是如果你遇到事件冒泡的问题,可以添加 e.Handled = true; 防止它超出此方法。 - esde84
在.NET 4.5和4.6中完美运行。 - Ed Bayiates

37

我正在使用类似这样的代码,只有在双击ListViewItem时才会触发事件,而不会在双击ListView标题时触发。

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject obj = (DependencyObject)e.OriginalSource;

    while (obj != null && obj != myListView)
    {
        if (obj.GetType() == typeof(ListViewItem))
        {
            // Do something here
            MessageBox.Show("A ListViewItem was double clicked!");

            break;
        }
        obj = VisualTreeHelper.GetParent(obj);
    }
}

3
在.NET 4.5中,这不再起作用。MouseButtonEventArgs不再有一个(原始的)source属性。 - Harmen
1
@Harmen,在 Net 4.5 中有任何解决方案吗? - PreguntonCojoneroCabrón
这是一个基于ListViewItem事件的解决方案:https://dev59.com/GXRB5IYBdhLWcg3wET5J - Kosau

10

使用ListView.HitTest方法

    private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        var senderList  = (ListView) sender;
        var clickedItem = senderList.HitTest(e.Location).Item;
        if (clickedItem != null)
        {
            //do something
        }            
    }

或者老方法

    private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        var senderList  = (ListView) sender;                        
        if (senderList.SelectedItems.Count == 1 && IsInBound(e.Location, senderList.SelectedItems[0].Bounds))
        {
            //Do something
        }
    }

    public  bool IsInBound(Point location, Rectangle bound)
    {
        return (bound.Y <= location.Y && 
                bound.Y + bound.Height >= location.Y &&
                bound.X <= location.X && 
                bound.X + bound.Width >= location.X);
    }

我在MSDN或我的编译器中没有看到e.Location作为成员。 - Ed Bayiates

7
    private void positionsListView_DoubleClick(object sender, EventArgs e)
    {
        if (positionsListView.SelectedItems.Count == 1)
        {
            ListView.SelectedListViewItemCollection items = positionsListView.SelectedItems;

            ListViewItem lvItem = items[0];
            string what = lvItem.Text;

        }
    }

2
ListViewItem lvItem = positionsListView.SelectedItems[0]; 这样更整洁。 - BaSsGaz

6

我的声望分数还不够高,不能在最有帮助的地方添加评论,但是这与那些询问.Net 4.5解决方案的人有关。

您可以使用鼠标的X和Y坐标以及ListView方法GetItemAt来查找被单击的项目。

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = myListView.GetItemAt(e.X, e.Y)
    // Do something here
}

5

这是针对Windows Forms中的ListView控件。WPF ListView控件在System.Windows.Controls中。 - skst

4
在这段代码中,我会双击 ListView。
    this.listView.Activation = ItemActivation.TwoClick;

    this.listView.ItemActivate += ListView1_ItemActivate;

ItemActivate指定用户如何激活项目

当用户双击时,将触发ListView1_ItemActivate。 ItemActivate属性是指访问所选项目的集合。

    private void ListView1_ItemActivate(Object sender, EventArgs e)
    {

        foreach (ListViewItem item in listView.SelectedItems)
           //do something

    }

它对我有效。


1
你可以先获取 ListView,然后获取所选的 ListViewItem。我有一个关于 ListBox 的示例,但 ListView 应该类似。
private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListBox box = sender as ListBox;
        if (box == null) {
            return;
        }
        MyInfo info = box.SelectedItem as MyInfo;
        if (info == null)
            return;
        /* your code here */
        }
        e.Handled = true;
    }

1

这不会忽略在 .net 4.5 或更高版本中错误地双击的情况。如果您选择一个项目并在其他地方双击,使用此代码将对所选项目执行操作。 - Ed Bayiates

1
以下是在 WPF listview 中获取双击的列表项的选定对象和对象匹配代码的方法:
/// <summary>
/// Get the object from the selected listview item.
/// </summary>
/// <param name="LV"></param>
/// <param name="originalSource"></param>
/// <returns></returns>
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.
    }
}       

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接