Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Property and Dependency Property

People also ask

What is a dependency property?

A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.

What is difference between dependency property and attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.

What's the differences between a CLR property dependency property and an attached property in WPF?

The Normal CLR property and the dependency property look quite similar but the dependency property is more powerful and has more features. In WPF, dependency properties are used in Animation, Styles, Triggers, Templates, Validation, Data Binding, Layout etc.

Why do we need dependency properties?

Why We Need Dependency Properties. Basically, Dependency Properties offer a lot of functionalities that you won't get by using a CLR property. CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.


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.

  1. 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.
  2. Data Validation : We can impose Data Validation to be triggered automatically whenever the property value is modified.
  3. 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.
  4. Participation in Styles : Styles are elements that defines the control. We can use Style Setters on Dependency property.
  5. Participation in Templates : Templates are elements that defines the overall structure of the element. By defining Dependency property, we can use it in templates.
  6. 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 about INotifyPropertyChanged, please read.
  7. CallBacks : You can have callbacks to a dependency property, so that whenever a property is changed, a callback is raised.
  8. Resources: A Dependency property can take a Resource. So in XAML, you can define a Resource for the definition of a Dependency property.
  9. 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.
  10. 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.