Wpf 更改DependencyProperties时调用DependencyObject的绑定

Wpf 更改DependencyProperties时调用DependencyObject的绑定,wpf,binding,dependency-properties,dependencyobject,Wpf,Binding,Dependency Properties,Dependencyobject,当内部DependencyProperties发生更改时,是否有方法通知DependencyObject的Bindings 例如,我有一门课: public class BackgroundDef : DependencyObject { public static readonly DependencyProperty Color1Property = DependencyProperty.Register("Color1", typeof(Co

当内部DependencyProperties发生更改时,是否有方法通知DependencyObject的Bindings

例如,我有一门课:

public class BackgroundDef : DependencyObject
    {
        public static readonly DependencyProperty Color1Property =
            DependencyProperty.Register("Color1", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public static readonly DependencyProperty UseBothColorsProperty =
            DependencyProperty.Register("UseBothColors", typeof(bool),
                typeof(BackgroundDef), new UIPropertyMetadata(false));

        public static readonly DependencyProperty Color2Property =
            DependencyProperty.Register("Color2", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public Color Color1
        {
            set { SetValue(Color1Property, value); }
            get { return (Color)GetValue(Color1Property); }
        }

        public bool UseBothColors
        {
            set { SetValue(UseBothColorsProperty, value); }
            get { return (bool)GetValue(UseBothColorsProperty); }
        }

        public Color Color2
        {
            set { SetValue(Color2Property, value); }
            get { return (Color)GetValue(Color2Property); }
        }
    }
为此,我有3个单独的双向绑定,用于设置Color1、Color2和UseBothColors的值。但是我还有一个BackgroundDef实例的绑定,它应该创建一个笔刷并绘制按钮的背景(单色或两种渐变色)。我的问题是DependencyProperties的双向绑定会更新属性,但不会调用类实例的绑定,因为整个对象显然不会更改。知道当DependencyProperties发生更改时如何调用DependencyObject的绑定吗?

您可以:

使用多重绑定并绑定到所有三个值,而不是绑定到类。然后,只要其中一个值发生更改,绑定就会重新计算。(这是我将使用的技术)

或:

如果您的类
BackgroundDef
是另一个类的属性,则只要
BackgroundDef
的任何属性发生更改,就可以在该类上引发NotifyPropertyChanged事件。当然,这意味着在
BackgroundDef
上有一个属性,即其父类,并在子类发生更改时通知父类