I want to call a method when a button is in the down stated (IsPressed) and another method when it is released. I can't seem to figure out a simple way to do this.
Thanks!
I want to call a method when a button is in the down stated (IsPressed) and another method when it is released. I can't seem to figure out a simple way to do this.
Thanks!
There are MouseDown and MouseUp events that you can handle to get a similar effect. But that won't handle the keyboard (spacebar) case. You'll have to do that with a KeyDown and KeyUp event.
Also be aware that a MouseDown event MAY not ever be followed by a MouseUp event, if the user moves the mouse after pressing down to a point where it is no longer over the button. (You can capture the mouse in MouseDown to guarantee you get the MouseUp event, I think)
Take a look at the Mouse class in the System.Windows.Input namespace. There are MouseDown and MouseUp attached events available.
If you're in a situation in which you can use bindings, set a OneWayToSource binding on the IsPressed property of the button. When the source property changes, execute your code.
This should work regardless of how the button is pressed.