Dependency properties are created the same way as properties.
Is a dependency property used only while creating a custom control?
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 him self , but depndent on another , let’z say 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.
For more Silverlight related concepts have a look at my Blog.
Advantages of Dependency Property
As a matter of fact a Dependency Property have a lots of advantages over the normal CLR properties.
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.
pls go throught this article
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 then) however it is on controls or at least FrameworkElements
where they are most useful.