I need to fire some event wh开发者_开发知识库en WPF button is pressed (by mouse, keyboard, touchscreen, etc) and to fire event when WPF buttons is unpressed.
How to do this? It should be easy but I can't find how to do this.
You can derive from Button and override the OnIsPressedChanged method and fire a custom event there.
Or you can bind to the ButtonBase.IsPressed property.
Another option is to use DependencyPropertyDescriptor:
var descriptor = DependencyPropertyDescriptor.FromProperty(Button.IsPressedProperty, typeof(Button));
descriptor.AddValueChanged(this.button, new EventHandler(IsPressedChanged));
If you are using MVVM, you can use event triggers to solve your problem. This way, you can still separate your UI requirements from your application logic.
- Example 1
- Example 2
精彩评论