What is Dependencyproperty and Dependency object

Dependency property can be linked to another property such that a change in the value of one changes the other.

From MSDN
The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree.

CLR property vs Dependency property:

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.

——————————————————————————

Dependency properies provide lots of addtional features/services to support Data Binding.

Once you create any property as a dependency property, then automatically you get following feature implemented for you. ie. Change Notification, Validation, Call Back, Inheritance, DataBinding, Styles, Default Values etc.

Dependency Object class:

If you need to implement all these features on your own for all properties where you need these feature, then it will be a big process and head ache for you. So, these all coming out of the box from Dependency Object class.

Basically dependency object class contains a dictionary. So, when ever set any value or retrieve value, then it will change the value or read from that Dictionary. So, it is nothing but a key value pair.

When you need your object to be dependency object?

You can create you object as dependency object if you need the object to be used in design, if you want to change value of the property from styles, animations etc from design place, if you want complete support for databinding etc.

In all other scenarios please go with normal properties.
References:

http://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property

Also read

https://sriramjithendra.wordpress.com/2014/09/12/choosing-dependencyproperty-or-inotifypropertychanged-in-wpf/
https://sriramjithendra.wordpress.com/2014/05/22/when-to-useuses-of-dependencyproperty/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s