Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wpf - Listen to DependencyProperty changed event and get the old value

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...