Skip to content

PropertyDependencies

Simon Cropp edited this page Jul 30, 2012 · 4 revisions

If you want to notify for other properties there are two ways to go about it using [Attributes].

However if you would like a more convention over configuration approach for many cases dependencies will be derived.

Automatic Properties

public class Person : INotifyPropertyChanged
{
    public string GivenNames { get; set; }

    public string FamilyName { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}

Field Gets

public class Person : INotifyPropertyChanged
{
    string givenNames;
    string familyName;
    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames
    {
        get { return givenNames; }
        set { givenNames = value; }
    }

    public string FamilyName
    {
        get { return familyName; }
        set { familyName = value; }
    }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", givenNames, familyName);
        }
    }
}

What gets compiled

All of the above cases will result in OnPropertyChanged("FullName") being called in the sets of GivenNames and FamilyName.

Clone this wiki locally