Поделиться через


Get notification from WPF dependency system

This is valuable information on hooking into DP system of WPF.

The answer is DependencyPropertyDescriptor. This is similar to PropertyDescriptor from WinForm.

https://msdn2.microsoft.com/en-us/library/system.componentmodel.dependencypropertydescriptor_members.aspx

For example, if you want to get notification whenever a property change but you don't own the code and you don't derive from the class and it does not expose SomethingChanged event. How can you get notification.

DependencyPropertyDescriptor dpDes = DependencyPropertyDescriptor.FromProperty(Control.BackgroundProperty);
dpDes.AddValueChanged(myElement, myEventHandler);

So whenever someone do a SetValue/ResetValue/ClearValue on myElement.Background, myEventHandler will be raised. When you are done listening to change notification, you can do RemoveValueChanged(..) to unsubscribe.

BenCon was talking about this as well :-)