开发者

Fire event when WPF button was pressed/unpressed

开发者 https://www.devze.com 2023-02-16 05:12 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消