What is a dependency property in .Net (especially in WPF context). What is the difference from the regular property?
3 Answers
The only explanation I found helpful and well written is this one: http://www.wpftutorial.net/dependencyproperties.html
Basically, DependencyProperties differ from regular properties in that they're not just setters / getters for fields in the class, but they retrieve their actual values dynamically during runtime. The SetValue()
method of DPs is pretty straightforward and sets the local value of the property to the value you gave it. However, when you try to GetValue()
from a DependencyProperty, it will first look for a local value, if none is present (which is viable in DependencyProperties unlike regular properties) it will continue up the logical UI tree until it will find such value. If the framework has reached the top of the tree without finding any local values, it will then use a predefined default value as the property's value.
This method allows DependencyProperties to consume less memory than regular properties since only values that were explicitly set by the user will be stored locally.
And, as mentioned above, DependencyProperties also allow us to bind to them in the XAML code and set triggers on them, which on regular properties is not allowed.
I hope I've managed to clear some of the vagueness :)

- 2,953
- 2
- 44
- 51
-
2And what's an "Attached Property"? – Paul-Sebastian Manole Jul 16 '13 at 12:04
-
15An Attached Property is a property that doesn't belong to the current item that is being declared, but affects another object. For example: `Grid.Row="1"` on a `Button` will set it to be in Row #2 on the parent `Grid` (due to the fact that rows are zero-based) but the `Row` DependencyProperty belongs to the `Grid` object. – Jonathan Perry Jul 17 '13 at 12:24
-
2When you said `DependencyProperties also allow us to bind to them in the XAML code and set triggers on them, which on regular properties is not allowed`, did you mean both binding in XAML and setting triggers or just setting triggers isn't allowed. Thank for the follow up and helping make things clearer! – Paul-Sebastian Manole Jul 31 '13 at 18:24
-
@Paul-SebastianManole DataTriggers work on regular properties also. Let's say you have a `Value` property (Not DP) with value 0 and you want your view to do something when `Value` changes to 1. You'll be able to do so using `DataTrigger` and setting the `Binding={Binding Path=Value, Value=1}` – Jonathan Perry Aug 01 '13 at 07:09
Dependency properties are properties of classes that derive from DependencyObject, and they're special in that rather than simply using a backing field to store their value, they use some helper methods on DependencyObject.
The nicest thing about them is that they have all the plumbing for data binding built in. If you bind something to them, they'll notify it when they change.

- 74,352
- 26
- 153
- 180

- 200,371
- 61
- 386
- 320
-
45That still tells me very little about what a dependency property can do, or why it exists. You mention nothing of their most valuable property, value resolution up the element tree. – ProfK Nov 19 '13 at 18:34
-
1hi @MattHamilton thank you for your answer - but what do you mean by "they use some helper methods on DependencyObject."? – BenKoshy Feb 19 '16 at 02:06
-
2@BKSpurgeon DependencyObject has some methods like "SetValue" and "GetValue" which you call to save/read the value of a dependency property, rather than using a backing field. – Matt Hamilton Feb 19 '16 at 02:12
http://techpunch.wordpress.com/2008/09/25/wpf-wf-what-is-a-dependency-property/ provides a good explanation of dependency properties both in the context of WF and WPF.
An excerpt:
Key Point – The Value of Dependency Properties Are Resolved
The ultimate goal of a dependency property, like any property, is to manage state. But unlike normal .Net properties, the local property value is not stored in an instance variable.
Instead, dependency properties are registered with the dependency property framework, and the underlying property value is resolved – meaning the value is determined by the dependency property framework based on rules defined by the property registration.