Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logically combine dependency properties

I'm using C# 4.0 and have created a DependencyObject MyView.

In MyView, I have two DependencyProperties, PropA and PropB, both are booleans.

I want a third DependencyProperty, PropC, also a bool, and simply put, should always give me (PropA || PropB).

  1. What is the best way to accomplish this?
  2. I was also thinking of making PropC a readonly DependencyProperty, but have read about issues with binding to readonly dp's (WPF ReadOnly Dependency Properties using MVVM)
like image 449
JoeSharp Avatar asked Jan 19 '11 21:01

JoeSharp


People also ask

How to add dependency property wpf?

To create new dependency property we need to follow the below procedure, Declare and register dependency property. For registered property set value using SetValue method and get value using GetValue method. Write a method to handle change done on dependency property.

What are the dependency properties?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.

Why do we need dependency properties?

Why We Need Dependency Properties. Basically, Dependency Properties offer a lot of functionalities that you won't get by using a CLR property. CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.


1 Answers

You can use the Dependency Property changed callback for PropA and PropB to set the value for PropC (don't use the CLR property wrapper for the Dependency Properties as they are never guaranteed to be called).

If you have these three DP's

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 property changed callback like this

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'll always update the value of PropC everytime PropA or PropB changes

Also, PropC doesn't need to be a DP, it can be a normal CLR property if you implement INotifyPropertyChanged. Then the implementation can look like this instead

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 could also bind PropC to PropA and PropB with a MultiBinding. Let me know if you want an example of this as well

like image 188
Fredrik Hedblad Avatar answered Nov 15 '22 09:11

Fredrik Hedblad