A dependency property provides functionality that extends the functionality of a property as opposed to a property that is backed by a field. Often, each such functionality represents or supports a specific feature of the overall WPF set of features. Hope this helps.
Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.
Dependency properties are used when you want data binding in a UserControl , and is the standard method of data binding for the WPF Framework controls. DPs have slightly better binding performance, and everything is provided to you when inside a UserControl to implement them.
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 :)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With