开发者

In WPF, what's the best way to get a PreviewPropertyChanged notification for a dependency property?

开发者 https://www.devze.com 2023-02-26 03:01 出处:网络
To my knowledge, there is no \'PreviewPropertyChanged\' that you can subscribe to for a dependency property, but I need to know a value WILL be changing.My thought is to sort of re-purpose the Coerce

To my knowledge, there is no 'PreviewPropertyChanged' that you can subscribe to for a dependency property, but I need to know a value WILL be changing. My thought is to sort of re-purpose the Coerce Value method and do my check there (naturally after any actual c开发者_开发百科oercion takes place since it makes sens that gets called before the actual change takes place. That way I can check the existing value versus the coerced value to determine a change, and if there is one, then fire my Preview' code. This the correct way to do this or am I missing something?


You got it. The CoerceValueCallback is a good way to raise an "XXXChanging" event, which gives consumers of the object a change to change the value or cancel the change. Your coerce would then return the new value, or the modified value. You can get the old value directly from the DependencyObject, as it's not included in the CoerceValueCallback parameters.

On a side note, "Preview" events are tunneling events. This means they go from the top of the tree down. Bubbling events go from the bottom up. So I would avoid calling it PreviewPropertyChanged, as that implies the property has already changed and it is a tunneling event.

0

精彩评论

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