Inside Dependency Properties
Wednesday, 28 October 2009
Article Index
Inside Dependency Properties
Custom Dependency Properties
Binding

Custom Dependency Properties

Now that we know the basic principle of the dependency property - it's an object oriented property - we can try creating a custom dependency property

Banner
.

The first complication is that we can't just create an instance of DependencyProperty the framework needs to know about it to allow it to take part in the common behaviours. The solution employed by its designers is to use a static method of Dependency property to create an instance and register it:

public static readonly 
DependencyProperty MyDPProperty =
DependencyProperty.Register(
"MyDP",
typeof(double),
typeof(MyClass));

The method call returns a DependencyProperty object set up to store a value of type double, with name MyDP and to act as a property belonging to MyClass.

This is how all dependency properties are created and they have to be public static readonly fields. The reason is that the property belongs to the class and it’s the implementation that sorts out the instance being used. That is there is only one property object shared among all of the instances of the class that uses it. Which particular instance value the property is working with is determined by "this" at runtime.

There are a number of overloaded Register methods but at the very least you have to specify:

  • the name of the property as a string "MyDP" in the example
  • the type of the property  typeof(double) in the example
  • the type of the class the property belongs to typeof(MyClass) in the example.

Notice that the static readonly field is generally given the same name as the property but with Property tagged on. Also notice that there are now two ways of referring to the property either by its name i.e. MyDP or by the variable MyDPProperty. You can also register a range of metadata values and callbacks but this is the minimum you need to create a custom dependency property.

Next we need the details of the class that the property belongs to. It can't be any class. It needs to inherit the methods needed to work with a dependency property and this means it has to inherit from DependencyObject or one of its descendants.

As C# only supports single inheritance you can see that this is quite a restriction and it is important that you choose the correct class to inherit from if you also want properties and methods of a particular control.

With these thoughts in mind we can now put together a complete class definition:

public class MyClass:DependencyObject
{
public static readonly
DependencyProperty MyDPProperty
= DependencyProperty.Register(
"MyDP",
typeof(double),
typeof(MyClass));
}

The class now has a static DependencyProperty as a field but we now have another small problem. The DependencyObject provides a setter and getter method but these infer the instance of the class from the current context. That is:

 GetValue(MyDPProperty)

and

 SetValue(MyDPProperty, value)

use "this" to determine the instance of the property that they should get or set. If you think about this you will realise that this means that you can only get and set a dependency property within the class or object that it belongs to. That is, an object cannot use them to access the dependency property on another object.

The solution is to use the dependency property to back a standard CLR property. For example:

public class MyClass:DependencyObject
{
public double MyProp
{
get
{
return (double) GetValue(MyDPProperty);
 }
set
{
SetValue(MyDPProperty, value);
}
 }

public static readonly
DependencyProperty MyDPProperty
= DependencyProperty.Register(
  "MyDP",
 typeof(double),
  typeof(MyClass));
}

Now when we assign or access MyProp it's actually MyDPProperty that is used. For example:

MyObject=new MyClass();
MyObject.MyProp = 3.141;
double pie = MyObject.MyProp;

stores the value 3.141 in MyDPProperty and retrieves the same value into pie.

You can think of the CLR property as a "wrapper" for the dependency property if it helps.

This is the usual pattern for implementing a custom dependency property.

Also notice that now we have three "names" for the property MyProp the CLR property, MyDP the Dependency property name and MyDPProperty the static variable referencing the DependencyProperty object. In most cases the CLR property would be given the same name as the dependency property. That is MyDP in this instance.

So for example a Slider object uses the name"Value" - as a CLR property, as the name of a dependency property and uses ValueProperty as the static variable referencing the DependencyProperty object.

<ASIN: 0672329859>



Last Updated ( Friday, 19 March 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.