Get the item doubleclick event of listview

后端 未结 16 1328
没有蜡笔的小新
没有蜡笔的小新 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:29

    I'm using something like this to only trigger on ListViewItem double-click and not for example when you double-click on the header of the 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:29

    You can get the ListView first, and then get the Selected ListViewItem. I have an example for ListBox, but ListView should be similar.

    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;
        }
    
    0 讨论(0)
  • 2020-12-01 02:30
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
        </Style>
    </ListView.ItemContainerStyle>
    

    The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g.

    private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListViewItem item = sender as ListViewItem;
        object obj = item.Content;
    }
    
    0 讨论(0)
  • 2020-12-01 02:30

    I found this on Microsoft Dev Center. It works correctly and ignores double-clicking in wrong places. As you see, the point is that an item gets selected before double-click event is triggered.

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
        // user clicked an item of listview control
        if (listView1.SelectedItems.Count == 1)
        {
            //do what you need to do here            
        }
    }
    

    http://social.msdn.microsoft.com/forums/en-US/winforms/thread/588b1053-8a8f-44ab-8b44-2e42062fb663

    0 讨论(0)
  • 2020-12-01 02:32

    I don't yet have a large enough reputation score to add a comment where it would be most helpful, but this is in relation to those asking about a .Net 4.5 solution.

    You can use the mouse X and Y co-ordinates and the ListView method GetItemAt to find the item which has been clicked on.

    private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListViewItem item = myListView.GetItemAt(e.X, e.Y)
        // Do something here
    }
    
    0 讨论(0)
  • 2020-12-01 02:34

    for me, I do double click of ListView in this code section .

        this.listView.Activation = ItemActivation.TwoClick;
    
        this.listView.ItemActivate += ListView1_ItemActivate;
    

    ItemActivate specify how user activate with items

    When user do double click, ListView1_ItemActivate will be trigger. Property of ListView ItemActivate refers to access the collection of items selected.

        private void ListView1_ItemActivate(Object sender, EventArgs e)
        {
    
            foreach (ListViewItem item in listView.SelectedItems)
               //do something
    
        }
    

    it works for me.

    0 讨论(0)
提交回复
热议问题