I’m new to WPF and I was just trying to run some code when a button is pressed (using any type of input device).
However when I looked at the Button events I saw quite a few that would match:
- MouseDown
- KeyDown
- StylusDown
- TouchDown
- PreviewMouseDown
- PreviewKeyDown
- PreviewStylusDown
- PreviewTouchDown
I don’t think Click is suitable for me since I need to know when the button is released.
So, do I have to write an event handler for each of these events or is there a simple way of including all input types?
Also what’s the difference between the “Preview” events and the regular ones (couldn’t find it on MSDN)?
I expect that you do want the Click event.
Note that the WPF Button has a ClickMode property that controls when the Click event is raised.
ButtonBase.ClickMode Property
The default value for the ClickMode property is Release which
Specifies that the Click event should be raised when a button is pressed and released.ClickMode Enumeration
Like Rachel said, the preview events occur before the actual events occur.
The Click event won’t work because you want events both when the button is pressed and released. You might be able to bind to the IsPressed property.
If you need events to be raised, you might be able use an EventSetter, instead of a Setter in the trigger.