监听DependencyProperty更改的事件并获取旧值 [英] Listen to DependencyProperty changed event and get the old value
问题描述
我有以下代码来订阅 Column
类的 VisiblePosition
属性的属性更改事件:
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);
}
这是 ColumnVisiblePositionChangedHandler $的定义c $ c>方法:
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?
谢谢,
推荐答案
这样注册属性更改的事件处理程序时,您不会获得旧的值信息。
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.
另一个解决方法是创建自己的依赖项属性(DP),并在DP和控件的DP之间创建绑定。这将为您提供WPF样式的更改通知。
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.
这篇关于监听DependencyProperty更改的事件并获取旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!