One of the cool features of dependency properties is that there is a nice, cheap and easy way to incorporate notifications using them. (Note, however, that a more sophisticated notification system may be required by your needs and would otherwise have to be implemented in a different way).
There’s this class called DependencyPropertyDescriptor. MSDN says, “Provides an extension of PropertyDescriptor that accounts for the additional property characteristics of a dependency property.” So, it inherits from the abstract class, PropertyDescriptor, which “Provides an abstraction of a property on a class.”
You can create a DependencyPropertyDescriptor object using its class’ static method “FromProperty”. Then, all you need to do is call the AddValueChanged method, passing in a control and a callback method, and then whenever that property’s value changes, the callback method will be invoked. The following code is a portion of the code-behind for the demo which can be downloaded by clicking here:
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty (FrameworkElement.HeightProperty, typeof(FrameworkElement) ); // Now add btnHi to the descriptor. Note that btnBye is not added. dpd.AddValueChanged(btnHi, HeightValueChanged);
The callback method is simply:
private void HeightValueChanged(Object sender, EventArgs e) { MessageBox.Show("height is now " + ((Button)sender).Height.ToString()); }
And the rest you can see in the download code.