Sunday, December 4, 2011

Steps for Creating a Dependency Property


Here are the steps for creating a Dependency property
1) Derive your class from DependencyObject class, and define a readonly static variable of type Dependency Property
Eg. public static readonly DependencyProperty FlavorProperty;

2) Add static constructor for parent class and using DependencyProperty.Regitser method register property created in Step 1
Eg.
static ParentClass()
{
 ParentClass.FlavorProperty = DependencyProperty.Register("Flavor",typeof(string), typeof(PieClass), md);
}

Steps 1 and 2 can be combined as
public static readonly DependencyProperty FlavorProperty = DependencyProperty.Register("Flavor",typeof(string), typeof(PieClass), md);


3) Create an instance property which for class which will wrap dependency property
public string Flavor
{
 get
 {
  return (string)GetValue(PieClass.FlavorProperty);
 }
 set
 {
   SetValue(PieClass.FlavorProperty, value);
 }
}
Same steps need  to be followed for creating attached properties by replacing DependencyProperty.Register by DependencyProperty.RegisterAttached method.

Here is the Dependency Property Setting Precedence list, I am just going to list their names which are self explanatory more details you can find from http://msdn.microsoft.com/en-us/library/ms743230.aspx
1) Property system coercion.
2) Active animations, or animations with a Hold behavior
3) Local value
4) TemplatedParent template properties
5) Implicit style
6) Style triggers
7) Template triggers
8) Style setters
9) Default (theme) style.
10) Inheritance.
11) Default value from dependency property metadata




No comments:

Post a Comment