我有一個包含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