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

154 Views Asked by At

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 coercion 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?

1

There are 1 best solutions below

3
On BEST ANSWER

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.