我有一个包含3个依赖属性A,B,C的类。这些属性的值由构造函数设置,每次属性A,B或C中的一个发生更改时,都会调用recalculate()方法。现在在执行构造函数的过程中,这些方法被调用3次,因为A,B,C三个属性都被改变了。 Hoewever这不是必须的,因为如果没有设置所有3个属性,方法重新计算()不能做任何真正有用的操作。那么,什么是属性更改通知的最好方法,但在构造函数中绕过这个更改通知?我曾想过在构造函数中添加属性改变通知,但随后DPChangeSample类的每个对象都会添加越来越多的更改通知。感谢您的任何提示!依赖属性,更改构造函数中的通知和设置值
class DPChangeSample : DependencyObject
{
public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));
public static DependencyProperty BProperty = DependencyProperty.Register("B", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));
public static DependencyProperty CProperty = DependencyProperty.Register("C", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged));
private static void propertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((DPChangeSample)d).recalculate();
}
private void recalculate()
{
// Using A, B, C do some cpu intensive calculations
}
public DPChangeSample(int a, int b, int c)
{
SetValue(AProperty, a);
SetValue(BProperty, b);
SetValue(CProperty, c);
}
}
非常感谢你,被证明是最好的答案(与VoodooChilds答案一起,我只是接受了这个,因为它包含了一个代码示例)。 – 2010-07-05 13:45:05