Dependency property is just a normal CLR property with extra features added.Some of these features are related to:
- Animation
- Data binding
- Styles
There could be a hierarchy of data sources for dependency property.The value of dependency property is calculated based on the data source with highest priority.For example animation data source has higher priority then data binding.
Dependency property are used just like a normal CLR property.Most of the properties defined by WPF elements such
as TextBox ,Label and other controls are dependency properties.If the normal CLR property doesn’t meet your requirements then you can define a dependency property by following the below steps:
1.Declare a class which will define the dependency property
public class SimpleDependencyClass { }
2. Declare a static readonly field of type DependencyProperty.
public class SimpleDependencyClass { public static readonly DependencyProperty SimpleDependencyProperty; }
You normally append the Property suffix in the dependency property name to differentiate it from CLR property.
3.You need to register the dependency property.This is like creating an object of a CLR property.To register the dependency property you use the Register() method of DependencyProperty in the static constructor of the class:
SimpleDependencyProperty=DependencyProperty.Register(“SimpleDependency”, typeof(string), typeof(SimpleDependencyClass))
You supply the following values to the DependencyProperty Register method:
- Name of the property
- Data type of property
- Data type of containing class.
Leave a Reply