What is Dependency Property in WPF ?

WPF introduces a new property called Dependency Property or shortly you may like to call it as DP. Dependency Property comes with some enhanced features in addition to normal CLR Properties.
A lot of features come with DP. A Dependency Property is a Property whose value depends on the external sources, such as animations, data bindings, styles, or visual tree inheritance. Not only this, but a Dependency Property also has changed, data binding and styling.

One important thing to keep in mind is that Dependency Properties are declared as static. Because Dependency Property is maintained and declared at class level. All the objects of the class use the single property declared on class level instead of using individually.




Let us talk about how Dependency Property is different from normal CLR properties in getting or setting values.

CLR Properties can only get (retrieve) or set (store) its value using the get/set methods of the class. Furthermore it is directly read from private members of the class.

Whereas DP can set its values from some external sources like animation, style, triggers etc by using a Value Resolution Strategy.

The purpose of Dependency Properties is to provide a way to compute the value of a property based on the value of other inputs.
These other inputs might include system properties such as :

  • themes
  • user preferences
  • just-in-time property determination mechanisms such as data binding and animations/ storyboards
  • multiple-use templates such as resources and styles, or
  • values known through parent-child relationships with other elements in the element tree.

In addition, a dependency property can be implemented to provide self contained validation, default values. In addition callbacks that monitor changes to other properties, and a system that can coerce property values based on potentially runtime information.

New Functionality/ Advantages of Dependency Property

  • Resources
  • Data Binding
  • Styles
  • Animations
  • Metadata overrides
  • Property Value Inheritance
  • WPF Designer Integration

Value Resolution Strategy

Dependency Property uses Value Resolution Strategy to resolve its value. In simple terms, this is a strategy which tells which value is finally going to set for the particular Dependency Property.

The precedence is high to low. Below diagram depicts the precedence dependency property follow to set its value.

Value Resolution Strategy