You can use the modified Dependency Property callback for PropA and PropB to set the PropC value (do not use the CLR wrapper for dependency properties, as they are never guaranteed to be called).
If you have these three DPs
public static readonly DependencyProperty PropAProperty = DependencyProperty.Register("PropA", typeof(bool), typeof(MyView), new PropertyMetadata(false, PropAPropertyChanged)); public static readonly DependencyProperty PropBProperty = DependencyProperty.Register("PropB", typeof(bool), typeof(MyView), new PropertyMetadata(false, PropBPropertyChanged)); public static readonly DependencyProperty PropCProperty = DependencyProperty.Register("PropC", typeof(bool), typeof(MyView), new PropertyMetadata(false)); public bool PropA { get { return (bool)this.GetValue(PropAProperty); } set { this.SetValue(PropAProperty, value); } } public bool PropB { get { return (bool)this.GetValue(PropBProperty); } set { this.SetValue(PropBProperty, value); } } public bool PropC { get { return (bool)this.GetValue(PropCProperty); } set { this.SetValue(PropCProperty, value); } }
you can use the modified callback property as shown
private static void PropAPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { MyView myView = source as MyView; myView.OnPropChanged(); } private static void PropBPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { MyView myView = source as MyView; myView.OnPropChanged(); } public void OnPropChanged() { PropC = PropA || PropB; }
This way you will always update the PropC value each time PropA or PropB changes
In addition, PropC does not have to be DP, it can be a common CLR property if you implement INotifyPropertyChanged . Then the implementation may look like this:
public void OnPropChanged() { OnPropertyChanged("PropC"); } public bool PropC { get { return PropA || PropB; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
You can also bind PropC to PropA and PropB using MultiBinding . Let me know if you want also an example of this.
Fredrik hedblad
source share