MouseMove works, MouseDown does not. WPF XAML

2019-07-21 09:26发布

问题:

I made Button in Xaml. I would like to fire MouseDown event:

    MouseDown="Button_MouseDown_1"

I implemented this method in codeBehind, but it doesn't work. But if I implement this method:

    MouseMove="Button_MouseMove_1"

Implementation works. Where is the problem ?

Seba.

回答1:

The Button element itself is handling the mouse down event before your event handler gets called - meaning your event handler wont get called.

More than likely what you're actually wanting to implement is the Click event though (e.g.):

Click="button1_Click"

This will respond to the button getting clicked by the mouse or if it has focus and enter is pressed etc...

But if you really do need to specifically implement a handler for the mousedown event on the button you can use the PreviewMouseDown event which your handler will be notified of.

MSDN: Routed Events Overview can give more detail of how routed events work.