DependencyProperty value change notification
Posted on June 9th, 2010 in Uncategorized | No Comments »
In order to get notified when a dependency property value changes, you can use the DependencyPropertyDescriptor.AddValueChanged function to register your own listener.
A simple example that displays the width of the window in a label:
public Window1()
{
InitializeComponent();
DependencyPropertyDescriptor widthDesc = DependencyPropertyDescriptor.FromProperty(Window.WidthProperty, typeof(Window));
if (widthDesc != null)
{
widthDesc.AddValueChanged(this, this.OnValueChanged);
}
}
private void OnValueChanged(object sender, EventArgs args)
{
txtLabel.Text = this.Width.ToString();
}