Dependency properties are created the same way as properties.
Is a dependency property used only while creating a custom control?
Dependency property is a property (not itself, but dependent on another, let’s say a XAML Binding property) which register another property.
The dependecy property register the other binding property in the code behind by registering it. A example that is used in my project is as follows:
public static DependencyProperty ImageUri = DependencyProperty.Register("Source", typeof(BitmapImage), typeof(CustomImagePlaceHolder), new PropertyMetadata(null));
In the above code the ImageUri, is a dependency property which register the Source, that is defined/declared inside generic.xaml (whatever not sure whether declared, defined or anything else) as follows:
..HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
/>
So here it is quite important that the template binding value in the XAML should be registered as dependency property in the code behind.
So when we have defined in XAML that the Image Source should be template bind with Source, we have registered the same Source As a DependencyProperty.
We have to say which type of dependency property is that, in above example the Source is the type of BitmapImage, so we have defined typeof(BitmapImage).
Now the owner/parent of this dependency property is our customControlClass CustomImagePlaceHolder, and we have defined that again while registering.
Now to set the value of depndency property, by using our properties as below:
public BitmapImage Source
{
get
{
string strURI = (string)GetValue(CustomImagePlaceHolder.ImageUri);
return new BitmapImage(new Uri(strURI));
}
set
{
SetValue(CustomImagePlaceHolder.ImageUri, value);
}
}
Now this is how it go, we set the value from our code behind or xaml to the source property defined above, and inturn it sets the value of the dependecy property ImageUri, which inturn sets the value in the template binding Source, as we have registered ImageUri as Source, that is presennt generic.xaml.
If you type propdp and hit the tab character in VS editor , then the auto-generated documentation of Dependency Properties is set as:
// Using a DependencyProperty as the backing store for MyProperty.
This enables animation, styling, binding, etc...
Advantages of Dependency Property
As a matter of fact a Dependency Property have a lots of advantages over the normal CLR properties.
INotifyPropertyChanged
whenever the value of the property is modified, DataBinding
is supported internally. To read more about INotifyPropertyChanged
, please read. In these, some of the features are only supported by Dependency Property. Animation
, Styles
, Templates
, Property value Inheritance etc could only be participated using Dependency property. If you use CLR property instead in such cases, the compiler will generate error.
please go through these articles,
http://www.codeproject.com/KB/WPF/BeginWPF4.aspx#diff
and http://www.dotnetfunda.com/articles/article961-wpf-tutorial--dependency-property-.aspx
and http://msdn.microsoft.com/en-us/library/cc221408(VS.95).aspx
Dependency properties and standard properties are quite different.
The key features delivered by dependency properties are support for binding and animation. If you want to assign a value to a property using a Binding
or template binding that property needs to be a dependency property. When animating a property the a dependency property can track both the current assigned value and the current animated value.
One other advantage that is often overlooked is that storage is only needed for properties that have values assigned. A typical control can have a lot of properties but its rare code that assigns a new value to all the properties, in fact, most of the properties are left at their default value and only few are actually set. With dependency properties the default values are stored as meta-data related to the property and do not require any memory allocated per control instance if the property remains unassigned.
Dependency properties are not limited to controls (anything derived from DependencyObject
can have them) however it is on controls or at least FrameworkElements
where they are most useful.
The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object.
I would suggest that if you are making a custom control or markup extension, you generally want to expose any of its public properties as dependency properties so that the consumer of your control can better manipulate the settings in XAML (without having to do it in code-behind).
If your property will commonly be the source of a databinding (e.g. providing the Text for a TextBlock), I would recommend using a standard CLR property and having the containing class implement INotifyPropertyChanged.
Further....
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.
Resources
Data binding
Styles
Animations
Metadata overrides
Property value inheritance
http://msdn2.microsoft.com/en-us/library/ms752914.aspx
Hope this helps.