3 つの依存関係プロパティ A、B、C を持つクラスがあります。これらのプロパティの値はコンストラクターによって設定され、プロパティ A、B、または C のいずれかが変更されるたびに、メソッド recalculate() が呼び出されます。コンストラクターの実行中に、これらのメソッドが 3 回呼び出されます。これは、3 つのプロパティ A、B、C が変更されるためです。ただし、メソッド recalculate() は 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);
}
}