C# Listbox Item Double Click Event

后端 未结 6 755
天命终不由人
天命终不由人 2020-12-08 06:00

I have a list box with some items. Is there anyway I can attach a double click event to each item?

Item 1
Item 2
Item 3

If i was to double

6条回答
  •  死守一世寂寞
    2020-12-08 06:41

    I know this question is quite old, but I was looking for a solution to this problem too. The accepted solution is for WinForms not WPF which I think many who come here are looking for.

    For anyone looking for a WPF solution, here is a great approach (via Oskar's answer here):

    private void myListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DependencyObject obj = (DependencyObject)e.OriginalSource;
    
        while (obj != null && obj != myListBox)
        {
            if (obj.GetType() == typeof(ListBoxItem))
            {
                 // Do something
                 break;
             }
             obj = VisualTreeHelper.GetParent(obj);
        }
    }
    

    Basically, you walk up the VisualTree until you've either found a parent item that is a ListBoxItem, or you ascend up to the actual ListBox (and therefore did not click a ListBoxItem).

提交回复
热议问题