views:

1211

answers:

4

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!

+1  A: 

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)

Andrew Arnott
A: 

Take a look at the Mouse class in the System.Windows.Input namespace. There are MouseDown and MouseUp attached events available.

Stu Mackellar
+3  A: 

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.

David Mitchell
I'm still fairly new to WPF. How is this done? Thanks
data binding is a pretty huge topic, but you may be able to get started with this: http://msdn.microsoft.com/en-us/library/ms752347.aspx. At the company where I work, we rarely directly manipulate or poll the state of GUI objects, preferring instead to rely on bindings.
David Mitchell
A: 

Certainly there is a better way then trying to intercept the mouse events. Some sort of trigger on the IsPressed propert?

Besides, I'm fairly sure you can't get some of the mouse events on a button as it is being intercepted by the system.