Maybe you wondered why DependencyObject class from WPF doesn’t implement INotifyPropertyChanged interface, but still (somehow almost magical) the WPF bindings work as expected on any DependencyProperty of a DependencyObject instance: the value updates are generating updates to the binding target.
Maybe at some point you will need to run some custom code when a DependencyProperty value changes for a DependencyObject instance, and of course, you may want to do that without a Binding. Well, how could you do that?
You need to use the DependencyPropertyDescroptor.FromProperty method and then the AddValueChanged method of its result to add your own event handler for value changes; it’s easy:
DependencyPropertyDescriptor.FromProperty(MyType.MyProperty, typeof(MyInstanceType)).AddValueChanged(myInstance, OnMyPropertyChanged);
private void OnMyTypeChanged(object sender, EventArgs e)
In the code above, MyType is the name of a type inheriting from DependencyObject that defines the DependencyProperty named MyProperty; MyInstanceType is the name of the type of myInstance object that you want to get the value change events for. Of course, many times MyType will be the same as MyInstanceType: these values can be different only for attached properties.