Search code examples
c#wpflistboxmouseleftbuttondown

MouseLeftButtonDown not recognized by a ListBox?


I'm encountering a huge problem, I have tried everything I could, but I didn't find any solution. I have a listBox, with a DataTemplate. I want to use the events MouseLeftButtonDown and MouseLeftButtonUp to check the item selected is the same the user clicked on.

The problem is the event MouseLeftButtonUp is recognized but not the event MouseLeftButtonDown.

Part of my XAML code :

<ListBox Grid.Row="1" MouseLeftButtonDown="listBox_Faits_MouseLeftButtonDown"
                      MouseLeftButtonUp="listBox_Faits_MouseLeftButtonUp">

The code behind :

    private void listBox_Faits_MouseLeftButtonUp(object sender, MouseEventArgs e)
    {
        ...
    }
    private void listBox_Faits_MouseLeftButtonDown(object sender, MouseEventArgs e)
    {
        ...
    }

Is anyone know why ?

Thanks you,

Regards,

Flo


Solution

  • This happens because the MouseLeftButtonDown event is getting handled by the list box item. To handle already handled events you can subscribe to it in code-behind and specify that you want to handle handled events, like this:

    listBox_Faits.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(listBox_Faits_MouseLeftButtonDown), true);