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
{
get
{
return (double)GetValue(ScalabilityProperty);
}
set
{
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.