Awarious OfficePortal - HRMS Application

What is Dependency Object in WPF? Where should I use it?

+1 vote
asked Jun 13, 2013 in WPF by anonymous

I do not understand what is the purpose of dependency object. What kind of features it provide in WPF programming? Should I create all my objects in as dependency object when I work in a WPF application?


1 Answer

+1 vote
answered Jun 15, 2013 by anonymous
selected Jun 18, 2013 by administrator
Best answer

Dependency object is the base object for all WPF objects. All the UI Elements like Buttons TextBox etc and the Content Elements like Paragraph, Italic, Span etc all are derived from Dependency Object.

Dependency objects are used for WPF property system. By default, what ever the property system we have in DOT Net CLR is very basic. But 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.

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.

Dependency objects can be created like below.

public class SampleDependencyClass : DependencyObject
        public static DependencyProperty ScalabilityProperty = 
           DependencyProperty.Register("Scalability", typeof(double), 
           typeof(SampleDependencyClass), new UIPropertyMetadata(0.0));

        public double Scalability 
                return (double)GetValue(ScalabilityProperty);
                SetValue(ScalabilityProperty, value);

First you need to register the dependency property using DependencyProperty.Register. In that method, you need to pass, "Name of property" (Scalability), "Data Type of that property" (typeof(double)), "Type of parent which hold the property" (typeof(SampleDependencyClass)) and the last parameter is the Metadata.

This metadata may contain different things, like default value, Callback Method when value change etc.In our example we have used default value. But we may use the Callback method also most of the time. Because if we need to get change notification, you can use this Metadata parameter to call the Callback mathod and raise change notification.

You may think you can implement change notification using INotifyPropertyChange interface on you SampleDependencyClass and implement OnPropertyChanged and raise event when the "setter" is called for Scalability property. Thats right and that will work if you setter is always called when the "Scalability" property value changed.

But the problem is, your property setter will not be called any time you change the value. It will be only called based on the way you change the value. For example, if you change the value from your code behind then your setter method of your property will be called. But, if you use your dependency property on the design page and if you change the value through the style operations in the XAML, then your setter will not be called. WPF has the mechanism to change the value directly inside the dependency property. So, you may not get the notification. Thats why we need to use callback method of the Metadata.

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.

commented Sep 13, 2013 by anonymous
Realy nice. My search regarding dependency property ended here.
commented Oct 4, 2013 by anonymous
Thanks. Good explanation
commented Jul 19, 2014 by anonymous

Is it good idea to use dependency object while binding large set of data?

commented Mar 13, 2015 by anonymous
whats the difference between dependency property and using a regular event handler
commented Mar 24, 2015 by Vish

Superb explanation .. !!!

commented Jul 23, 2015 by administrator (315 points)
Could you please explain your scenario more clear? Are you asking whether we can create dependency object to hold large data which can be bound with binding property?
commented Jan 18, 2016 by Mike

Thank you for this exceptional answer - clear, simple and easy to understand. I've been using WPF for years and never needed DP's until recently but I got confused and frustrated with so many of the articles about DP's I found as they nearly all get bogged down in the tiny details - so it's nice to get a great explanation like this as a basis for understanding the fundamentals of this incredibly powerful part of WPF.

commented Mar 30, 2016 by anonymous
its very useful.

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2019