How to capture a mouse click on an Item in a ListBox in WPF?

前端 未结 6 1365
长情又很酷
长情又很酷 2020-12-02 11:30

I want to get notified when an item in a ListBox gets clicked by the mouse, whether it is already selected or not.

I searched and found this: (http://kevin-berridge.

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 11:44

    There is another way to get MouseDown event in ListBox. You can add event handler for events that are marked as handled by using handledEventsToo signature of AddHandler method:

    myListBox.AddHandler(UIElement.MouseDownEvent, 
            new MouseButtonEventHandler(ListBox_MouseDown), true);
    

    Third parameter above is handledEventsToo which ensures that this handler will be invoked no matter if it is already marked as Handled (which ListBoxItem does in ListBox).
    See Marking Routed Events as Handled, and Class Handling for explanation.
    See How to Attach to MouseDown Event on ListBox for example.

提交回复
热议问题