#114 – How Dependency Properties Are Implemented
November 3, 2010 1 Comment
Dependency properties in WPF can be used by a client just like regular CLR properties, but they are implemented differently in the defining class. The added complexity serves to support features like data binding, property inheritance and change notification.
Classes that want to implement a dependency property start by by inherting from DependencyObject, which provides the support for reading and writing dependency property values.
The class then declares a static variable of type DependencyProperty for the new property. A static instance is created using the DependencyProperty.Register function. This instance does not store property values, but stores metadata about the property.
public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(Person));
Dependency property values are managed by the GetValue and SetValue methods inherited from the DependencyObject class.
public int Age { get { return (int)GetValue(AgeProperty); } set { SetValue(AgeProperty, value); } }