31

When should I use dependency properties in WPF?

They are static, so we save a lot on memory, compared to using .NET properties. Other gains of using dependency properties over .NET properties are: 1) no need to check thread access 2) prompt a containing element to be rendered etc...

So it seems I should ALWAYS use dependency properties in my projects where I use WPF?

Maybe for some trivial properties of helper classes here and there I could get away with .NET properties...

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
user2381422
  • 5,645
  • 14
  • 42
  • 56
  • http://stackoverflow.com/questions/5193144/what-is-a-dependency-property-and-when-is-it-used, http://stackoverflow.com/questions/617312/what-is-a-dependency-property – Nitesh Sep 03 '13 at 12:48
  • One important thing is, dependency properties can only be used on objects inheriting from DependencyObject. One of the reasons not to use them on ViewModels and therefore not always in a WPF project. Also they have a small performance impact. – dowhilefor Sep 03 '13 at 13:04
  • I'm not sure why you say "no need to check thread access"?. As far as I know, if you set the value of a dependency property from another thread than the UI thread then an exception will be thrown. – AH. Sep 24 '14 at 12:06
  • Dependency properties are not static and don't save any memory. Only the definition of the property is static. All dependency properties are tied to an object reference so the property does take up a piece of memory for each instance. – Christian Findlay Jun 05 '18 at 01:52

6 Answers6

20

Dependency Property is a broad concept to explain which might take couple of pages to write. So just to answer your main question, Dependency property is used where

  1. You know the property is going to be the binding target i.e you are creating a user control/custom control and want property that should be binding driven.

  2. You want automatic property change notifications (coerse and validation also).

  3. We want value inheritance in styles,themes, parent or default value.

  4. There is not need to create the properties on Model or ViewModel layer as dependency properties most of the time as that is not going to help much on memory saving front as most of the properties we define in model/VM will have values per instance as they will be constantly changing. Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.

Thanks

Noam Ohana
  • 186
  • 14
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • Yes styles and animations are one context where you MUST use dependency properties. – user2381422 Sep 03 '13 at 13:02
  • it should also be used when you want to be able to bind its value through XAML. – Ahmed Ahmed Sep 03 '13 at 13:12
  • This is not a complete answer. You basically need to define dependency properties for all properties that you want to use in XAML. This is the main point of difference with Silverlight which allows you to use normal properties in XAML. – Christian Findlay Jun 05 '18 at 01:51
18

The main reason to create DependencyProperty is when you write you own WPF control. DependencyProperties can be used as binding source and target, and can be animated. The properties of all framework's controls are implemented as DependencyProperty, that why you can make powerful data binding in XAML.

However in most situation, like in with the MVVM pattern, you don't need dependency properties, INotifyPropertyChanged is enough.

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
11

The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.

When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

The advantages of dependency properties are

  1. Reduced memory footprint:
    It's a huge dissipation to store a field for each property when you think that over 90% of the properties of a UI control typically stay at its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values are stored once within the dependency property.
  2. Value inheritance:
    When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value.
  3. Change notification:
    Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding.

check the below url for more details about the magic behid it

dependency properties in WPF

Ahmed Ahmed
  • 1,036
  • 11
  • 16
  • 15
    You basically copied here the msdn documentation without giving a useful comment. Yes all these things are good, but what is your suggestion, to always use dependency properties? – user2381422 Sep 03 '13 at 12:53
7

enter image description here

CLR Property vs. Dependency Property

A CLR property reads directly from the private member of the class. The Get() and Set() methods of the class retrieve and store the values of the property. Whereas when you set a value of a Dependency Property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

Advantages of a Dependency Property Less memory consumption

The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free.

Property value inheritance It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.

Change notification and Data Bindings Whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.

Participation in animation, styles and templates A Dependency Property can animate, set styles using style setters and even provide templates for the control.

CallBacks Whenever a property is changed you can have a callback invoked.

Resources You can define a Resource for the definition of a Dependency Property in XAML.

Overriding Metadata You can define certain behaviours of a Dependency Property using PropertyMetaData. Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.

  • "A CLR property reads directly from the private member of the class." this is so completely wrong. A CLR property maps to getter / setter functions. – cdiggins Nov 02 '20 at 00:14
3

Perhaps you should take another look at the Dependency Properties Overview page at MSDN.

Personally, I would only ever create a DependencyProperty when I really need to. For the most part, I use normal CLR properties in data type and view model classes to bind to... this is absolutely fine, as long as I implement the INotifyPropertyChanged interface.

So for all of my usual data bindings, I use normal CLR properties. I only declare a Dependency Property in order to provide some added functionality in a UserControl.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
2

Dependency properties are used when you want data binding in a UserControl, and is the standard method of data binding for the WPF Framework controls. DPs have slightly better binding performance, and everything is provided to you when inside a UserControl to implement them.

Otherwise, you typically use INotifyPropertyChanged for binding elsewhere, since it's easier to implement in stand-alone classes and has less overhead. As far as your original assumptions:

  1. There is a local instance of your variable, you definitely do not save any overhead over a property since there is significant data binding logic built-in.
  2. They must be accessed on the main thread.
Community
  • 1
  • 1
Will Eddins
  • 13,628
  • 5
  • 51
  • 85
  • Thats not correct, at least its not precise. Yes, dependency properties can be used on UserControls but also on CustomControls and basically every object deriving from DependencyObject. – dowhilefor Sep 03 '13 at 13:02