Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use dependency properties in WPF?

Tags:

c#

wpf

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...

like image 504
user2381422 Avatar asked Sep 03 '13 12:09

user2381422


People also ask

Why do we need dependency property in WPF?

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.

What is dependency property in WPF?

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a type's property. Collectively, these services are referred to as the WPF property system. A property that's backed by the WPF property system is known as a dependency property.

What is the difference between property and dependency property?

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.

Why dependency property is better than 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.


2 Answers

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

like image 57
Nitin Avatar answered Oct 12 '22 13:10

Nitin


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.

like image 33
Benoit Blanchon Avatar answered Oct 12 '22 14:10

Benoit Blanchon