What is the difference between Property and Dependency Property
Dependency properties are created the same way as prop开发者_如何学Goerties.
Is a dependency property used only while creating a custom control?
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.
Advantages of Dependency Property
As a matter of fact a Dependency Property have a lots of advantages over the normal CLR properties.
- Property Value Inheritance : By Property Value Inheritance we mean that value of a Dependency property can be overridden in the hierarchy in such a way that the value with highest precedence will be set ultimately.
- Data Validation : We can impose Data Validation to be triggered automatically whenever the property value is modified.
- Participation in Animation : Dependency property can animate. WPF animation has lots of capabilities to change value at an interval. Defining a dependency property, you can eventually support Animation for that property.
- Participation in Styles : Styles are elements that defines the control. We can use Style Setters on Dependency property.
- Participation in Templates : Templates are elements that defines the overall structure of the element. By defining Dependency property, we can use it in templates.
- DataBinding : As each of the Dependency property itself invokes
INotifyPropertyChanged
whenever the value of the property is modified,DataBinding
is supported internally. To read more aboutINotifyPropertyChanged
, please read. - CallBacks : You can have callbacks to a dependency property, so that whenever a property is changed, a callback is raised.
- Resources: A Dependency property can take a Resource. So in XAML, you can define a Resource for the definition of a Dependency property.
- Metadata overrides : You can define certain behavior of a dependency property using PropertyMetaData. Thus overriding a metadata form a derived property will not require you to redefine or reimplementing the whole property definition.
- Designer Support : A dependency property gets support from Visual Studio Designer. You can see all the dependency properties of a control listed in the Property Window of the Designer.
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 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.
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.
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...
精彩评论