Listen to DependencyProperty changed event and get the old value

sean717 picture sean717 · Jan 12, 2012 · Viewed 18.6k times · Source

I have the following code to subscribe to property changed event for VisiblePosition property of Column class:

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ColumnBase.VisiblePositionProperty, typeof(Column));

if (dpd != null)
{
   dpd.AddValueChanged(col, ColumnVisiblePositionChangedHandler);
}

Here is the definition of the ColumnVisiblePositionChangedHandler method:

static internal void ColumnVisiblePositionChangedHandler(object sender, EventArgs e)

The problem is I need to get the old value of the property. How do I do that?

Thanks,

Answer

Libor picture Libor · Jul 25, 2013

Unfortunately, you don't get old value information when registering property changed event handler this way.

One workaround is to store property value somewhere (this is your 'old' value) and then compare it to current value in the event handler.

Another workaround is to create your own dependency property (DP) and create binding between your DP and the control's DP. This will give you change notification in the WPF style.

Here is an article about this.