4

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

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Flo
  • 261
  • 1
  • 3
  • 6

1 Answers1

13

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);
Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • I was going to suggest trying the PreviewMouseLeftButtonDown event also, but this way gives you more control. – Tom Jan 07 '11 at 09:43
  • Hum... Pavlo, I tried your solution, but the event is not recognized! – Flo Jan 07 '11 at 10:17
  • It should work. Please check again. Don't forget to pass "true" to the last parameter of AddHandler! Also, as @Tom mentioned, you can subscribe to the PreviewMouseLeftButtonDown event. – Pavlo Glazkov Jan 07 '11 at 10:26
  • I checked a thousand time. But I trust your solution, it should work... So i'm gonna check it a last time! (and i'll check all my code, to be sure... because there is no reason explaining why it doesn't work) Thanks, I warn you if it works! – Flo Jan 07 '11 at 10:29
  • Ow god, it works... The problem is that it created a second MouseLeftButtonEvent handler, with a MouseButtonEventArgs... But I was using a MouseEventArgs... Thank you very much Pavlo! – Flo Jan 07 '11 at 12:51