WPF Dependency Properties

Posted by Al on Sun, April 17 at 14:50:04 in C#

Traditionally, a property in .NET is a member variable with associated get and set methods that are used to read and write its value.

Ex.Traditional .NET Property

private int mEmployeeRating;

 

public int EmployeeRating

{

    get { return mEmployeeRating; }

    set { mEmployeeRating = value; }

}

WPF introduces the concept of a dependency property, which also uses get and set methods to read and write its value, but instead of storing the property value in a member variable, instead stores its value in a dictionary maintained by the property system in WPF.  In other words the back store for a traditional property is a member variable and the back store for a dependency property is a dictionary maintained by the property system.  This gives the property system the ability to manipulate the value returned by the property.

So why would you want to use a dependency property?

Dependency properties give you additional functionality that traditional properties do not supply.  Dependency properties allow you to:

  • Set the property value using resources.
  • Bind the property to a data source, so that property value gets update dynamically whenever the data source value changes.
  • Set the property value …

    Read more