I’m currently using the following code to be notified when a DependencyProperty‘s Value has changed:
DependencyPropertyDescriptor propDescriptor = DependencyPropertyDescriptor.FromProperty(property, control.GetType());
propDescriptor.AddValueChanged(control, controlChangedHandler);
This works great and is quite simple, but what I really need now is to be notified when a DependencyProperty‘s Value is about to change. I thought there would be a DependencyPropertyDescriptor.AddValueChanging() method, but it doesn’t seem to exist. Any ideas how I can create this functionality?
I need to be able to cancel the change, fire off some asynchronous backend logic, and only have the control’s property really change if the backend logic succeeds.
I solved the problem at hand by implementing wrapping my IODevice in an
INotifyPropertyChangedimplementation and binding it to theDependencyProperty.The magic is in the fact that
IODeviceWrapper.Value‘s setter doesn’t actually set the value, but rather does the IO. It turns out that when the setter is called by theDependencyPropertyit’s bound to, the change hasn’t yet been committed to theDependencyProperty‘s value. Hence,IODeviceWrapper.Value‘s setter gets called in by theDependencyProperty‘s sudo-, non-existentValueChangingevent.At this time, if the
DependencyPropertyreads from theValue‘s getter it will get the old value until the IO is complete. When the IO is completeIODeviceWrapper.Value‘sPropertyChangedevent gets fired, and theDependencyPropertythen reads the new value.My flawed design is now working flawlessly. Here’s the code in case anyone else is interested. Ignore the naysayers.