views:

29

answers:

1

Is there a way to notify a DependencyObject's bindinigs when the inner DependencyProperties have changed?

For example, I have this class:

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); }
        }
    }

For which I have 3 separate two-way bindings that set the values for Color1, Color2 and UseBothColors. But I also have a binding for a BackgroundDef instance, which should create a Brush and draw the background of a button (either a single color, or two gradient colors). My problem is that the two-way bindings for the DependencyProperties update the properties, but the binding for the class instance is not called, as apparently the entire object does not change. Any idea how I could call the bindings for the DependencyObject when the DependencyProperties are changed?

+1  A: 

You could:

Use a multibinding and bind to all three values instead of binding to the class. then whenever one of the values changes the binding will re-evaluate. (this is the technique i would use)

Or:

If your class BackgroundDef is a property of another class, you could raise a NotifyPropertyChanged event on that class whenever any of BackgroundDef's properties change. Of course this would mean having a property on BackgroundDefthat is its parents class and notifying the parent whenever the child had changed.

Aran Mulholland