views:

240

answers:

1

I'm reading WPF Recipes in C# 2008:

http://www.apress.com/book/view/9781430210849

and starting on the third recipe they asssume you know how dependency properties work.

So after a little googling, I understand in general that these are properties of an object which when placed inside another object "adapts to the context" to the host object. I also "learned" that "you never really know what the value of these properties are since they depend on their context."

But still much of what is being described in the book leaves me with no idea how I could use these when building applications.

Who has a good metaphor or example of dependency properties for people starting out with them and wanting to know when and how they would use them?

+2  A: 

Dependency properties are just like normal properties except they have some special "hooks" that WPF uses.

One special thing is that sometimes if you don't set a property value it will receive its value from the control it is placed in (so if you set the font for a button the text block inside the button will use this font unless you specified a different font for the text block), I assume this is the source of all the "never know the value" nonsense.

If you are writing a WPF control you probably should use dependency properties because you can specify if changes should automatically cause the control to re-render itself (and more) and you can use them for data binding.

If you are writing a class derived from Freezable (directly or indirectly) using only dependency properties will save you some work.

If you are writing a class that is not WPF specific then there is probably no reason to use dependency properties.

Nir