views:

296

answers:

5

I'm trying to figure out what exactly Dependency Properties are, but when I look anywhere for a definition, I only find "how to use" but not "what it is". Imagine you are asked on a job interview - what is a dependency property. What would be your answer?

A: 

MSDN provides a good definition, description and examples

For more deep understanding of DependencyProperty check here

viky
+1  A: 

"gives you a bunch of infrastructure to do all the things that you often want to do with a normal property - validate it, coerce it into a proper range, give out change notifications, and a number of other aspects."

WPF Tutorial - Introduction To Dependency Properties

Bermo
Validation, coercion and change notification are all possible with regular CLR properties as well
Gurdas Nijor
A: 

A dependency property is a property that is backed by the WPF property system instead of by a field in the declaring class.

The significance of this is that, because WPF owns the property, WPF can factor in various considerations when calculating the property value -- such as animations, styles and data bindings. Another consequence is that because properties are managed by WPF they don't have to be declared on the classes that conceptually have the state: hence, atttached properties, which allow e.g. a Grid to associate Grid-specific state with non-Grid objects.

(By the way, I've mentioned WPF above because this is the main framework that uses DPs, but Windows Workflow Foundation also has the notion of dependency properties. So to be strictly correct a DP is a property that is backed by an external property system, specifically one which allows factors other than "the last set value" to come into play when getting the property value.)

itowlson
+5  A: 

A DependencyProperty is a property whose value depends (or can depend) on some other source (such as animation, data binding, styles, or visual tree inheritance). A regular property's value is stored in the object it belongs to, while you can think of a dependency property as being stored in a database somewhere. This database is essentially composed of a dictionary that maps (object, property) pairs to their values, along with a mapping of which properties depend on other properties (e.g. so when you change the DataContext of a Panel, it can notify all the children inside the panel).

So why do they store property values in some magic database somewhere? There are a few reasons:

  • It reduces storage space. Adding a property (even if its value is null) to a class adds 4 bytes of space to every instance of the class. A DependencyProperty only takes up space when an instance has a value. For example, a FrameworkElement has dozens of dependency properties, most of which are never assigned values. If all those properties were stored in the class, each instance would be hundreds of bytes. Instead each instance is only about 40 bytes.

  • It enables attached properties. Properties like Canvas.Left and Grid.Row have to be stored on objects that have never heard of a Canvas or Grid. So where do you put them? You put them in a database somewhere.

  • It enables automatic property changes. Imagine how you would implement something like styles or property inheritance (the ability to set something like a font or data context on a parent element and have its value propagate to all child elements). Having all of this stored in a database makes it so the code is all in one place instead of being implemented separately for each object and property that needs it.

Gabe
With reference to the first point, how did the byte space problem never came up in winforms which has many properties in its controls.
thewpfguy
thewpfguy: Winforms controls are backed by hWnds which have so much overhead that the number of bytes taken up by miscellaneous events and properties is irrelevant.
Gabe
That is exactly the point. In Winforms, even with lots of overhead - hwnd, event, properties (some never assigned) - the memory usage was never a problem. If it really was a problem, then this design backing the dep. prop. could have been implemented there as well. I see that this efficient memory space benefit is quoted in many books, articles but it is solving a problem which didn't really exist or didn't matter. I believe even if this is one of the benefits of dep. property it should be the least important one.
thewpfguy
thewpfguy: You have to remember that even though RAM is much cheaper than it used to be, page faults are much more expensive than they used to be. Nobody's going to care that you saved 4MB of memory, but lots of people will notice if you save 1000 page faults. Also, Winforms has `OnPaint`, so if anything got too complicated you could always fall back to that. If you need to show text, you just draw it from OnPaint. WPF has no OnPaint, so you might need to be able to represent text with a different object for each glyph, for example.
Gabe
A: 

What is it for?

A dependency property is a property where the current value is dependent (hence the name) on other aspects such as default values, validation, coercion, value inheritance or animation.

Also a depedency property has inbuilt support for change notifications, data binding and styling.

What are they?

A bunch of classes and defined ways to use them that provide the above functionality to be used on classes that inherit from DependencyObject.

They are not a language feature. They are a feature of the .NET Framework.

Patrick Klug