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




So far you have no proof that the MyProp CLR property really is backed by a dependency property - so let's demonstrate that it can be bound to a slider control.

It is assumed that you know the basics of data binding using code but don't worry if you don’t because it's easy and follows the XAML as long as you remember the XAML is just an object instantiator and initializer.

First we need to create a binding object initialised to the source object, i.e. MyObject in this case:

Binding myBinding = new Binding("MyDP");
myBinding.Source = MyObject;

Notice that the name of the property, i.e. MyDP is used rather than the static variable MyDPProperty. One of the problems with working with dependency objects is trying to guess which name or reference is required.

The binding object defines and controls the source of the data and all that remains is to connect it to the target using the SetBinding method, assuming the target provides one:


and assuming that there is a Slider control on the form called slider1.

Notice that in this case you have to provide not the name of the property but the static variable referencing it, i.e. ValueProperty rather than Value. Also notice that the WPF framework doesn't use the get/set methods to access the property but goes straight to the dependency property. If you need to intercept a WPF update to a dependency property you can add a call back function to the metadata in the Register method.

If the object doesn't provide a SetBinding method you have to do the job using the BindingOperations static object:


The result is the same.

Now MyDP dependency property is bound to slider1's ValueProperty dependency property. If you now do:

MyObject.MyProp = 3.141;

you will see the slider move on the form to a position that corresponds to 3.141.

You can also do the job the other way round with MyObject as the source and slider1 as the target:

Binding myBinding = new Binding("Value");
myBinding.Source = slider1;

but in this case you have to use BindingOperations because the property doesn't implement a SetBinding method - but it could. Now when you change the slider's position the value of MyDP is changed accordingly.

Of course you could set a two-way data binding using the Mode property and then which is the source and which is the target doesn’t make a lot of difference.

Can you use the MyClass and the MyDP dependency property in XAML?

Yes of course - but that is another story.



WriteableBitmap gives you a bitmap object that you can modify dynamically - but exactly how to do this isn't always obvious.

Simple WPF data binding

Find out how WPF data binding really works. It's not the binding object that matters - it's the binding expression.

Bitmap Coding and Metatdata in WPF

Having looked at working with raw pixel data we turn our attention to formattted image files and how to code and decode both the pixel data and the meta data they contain.


Bitmap Effects

WPF bitmap effects are easy to use but subtle. Before moving on to consider custom effects we take a careful look at what is provided as standard.

BitmapImage and local files

When and how to use the BitmapImage class

Other Articles


Last Updated ( Friday, 19 March 2010 )

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