Logically combine dependency properties

I am using C # 4.0 and created DependencyObject MyView.

In MyView, I have two DependencyProperties, PropA and PropB, both of which are Boolean.

I want the third DependencyProperty, PropC, also a bool, and just set it, should always give me (PropA || PropB).

  • What is the best way to do this?
  • I also thought about getting PropC to read only DependencyProperty, but read about readonly dp binding issues ( WPF ReadOnly Dependency Properties using MVVM )
+6
wpf dependency-properties
source share
2 answers

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.

+3
source share

The linked webpage is for an unusual situation, a push binding. That is, one-way binding was attempted for a read-only property, and not for another property trying to bind it. In contrast, if you want your property to bind to other properties using a one-way binding expression for another property , you can use the read-only dependency property without any problems.

Edit:

Here is an example:

 <Grid> <Grid.Resources> <local:MyObject x:Key="myObject" PropertyA="True" PropertyB="False"/> </Grid.Resources> <StackPanel DataContext="{StaticResource myObject}"> <CheckBox IsChecked="{Binding PropertyA}" Content="PropertyA"/> <CheckBox IsChecked="{Binding PropertyB}" Content="PropertyB"/> <CheckBox IsChecked="{Binding PropertyC, Mode=OneWay}" IsEnabled="False" Content="PropertyC"/> </StackPanel> </Grid> 

and dependency properties, one of which is read-only:

 public class MyObject : DependencyObject { public bool PropertyA { get { return (bool)GetValue(PropertyAProperty); } set { SetValue(PropertyAProperty, value); } } public static readonly DependencyProperty PropertyAProperty = DependencyProperty.Register("PropertyA", typeof(bool), typeof(MyObject), new UIPropertyMetadata(false, OnPropertyAOrBChanged)); public bool PropertyB { get { return (bool)GetValue(PropertyBProperty); } set { SetValue(PropertyBProperty, value); } } public static readonly DependencyProperty PropertyBProperty = DependencyProperty.Register("PropertyB", typeof(bool), typeof(MyObject), new UIPropertyMetadata(false, OnPropertyAOrBChanged)); public bool PropertyC { get { return (bool)GetValue(PropertyCProperty); } set { SetValue(PropertyCPropertyKey, value); } } private static readonly DependencyPropertyKey PropertyCPropertyKey = DependencyProperty.RegisterReadOnly("PropertyC", typeof(bool), typeof(MyObject), new UIPropertyMetadata()); public static readonly DependencyProperty PropertyCProperty = PropertyCPropertyKey.DependencyProperty; private static void OnPropertyAOrBChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var myObject = d as MyObject; myObject.PropertyC = myObject.PropertyA || myObject.PropertyB; } } 
0
source share

All Articles