开发者

Listening for changes to IsEnabled on a Wpf Element

I want to listen for changes to the IsEnabled property on WPF elements (so that I can run some common code whenever it changes) without having to put a load of code in each window/page.

I also want to avoid any potential problems with memory leaks due to strong event listeners.开发者_JS百科 I've come across some articles that suggest using weak event listeners etc. but that seems awfully complicated for something that seems like it should be really easy.

I don't want to have to subclass controls in order to do this as there are several control types (and probably more in future) that I want to listen for the IsEnabled change on.

Has anyone come up with a neater way of handling this?


There is a Control.IsEnabledChanged event.


A neat solution to this problem would to create a custom attached property and setup a one way binding with the IsEnabled property as the source:

<Control IsEnabled={Binding IsEnabledProperty}
         AttachedProperty={Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Mode=OneWay}"/>

This allows you to handle the common functionality in the attached property's changed handler (which could involve firing a custom routed event as IsEnabled is a regular CLR event and won't bubble up).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜