developer
developer

Reputation: 5478

Dependency Properties

Can anybody explain me what a dependency property is in WPF and what is its use. I know there are a lot of tutorials on google for it, but they teach how to create a dependency property. I am confused as to where I would use it. I mean will I use it in XAML? If anybody could explain me in simple terms, that would be great.

It would be nice if a simple example is shown along with XAML, with example, of how I might use the property, and what would be the effect after I use it. Thanks a lot for all your answers..

Upvotes: 7

Views: 1863

Answers (5)

Reed Copsey
Reed Copsey

Reputation: 564791

The many links listed should give you a good idea of what dependency properties are, but in general, the easiest way to think about them I believe is the following:

Dependency properties are what you need to use for properties of user interface elements, if you want to be able to bind them using WPF's data binding. In order to be the "Target" of a data binding operation, you'll need to make the property a Dependency Property.

When you're implementing a standard class (which becomes the DataContext of a "control"), you'll want to use INotifyPropertyChanged instead of DPs. This allows that class to be a binding "Source".

In general, you'll only want to make Dependency Properties if you're making something that will be bound in XAML, as the Target of a UIelement. For example, say we have XAML like this:

<local:MyControl ControlProperty="{Binding SomeProperty}" />

Normally, ControlProperty will be a Dep. Property, since it's the binding target, and SomeProperty will be a standard CLR property (not a DP), in a class that implements INotifyPropertyChanged.

Upvotes: 13

herzmeister
herzmeister

Reputation: 11297

A Dependency Property does not store its value in a field, but rather in some hashtable. Thus it needs less memory, which is important especially for GUI objects, because most properties will retain their default values und thus those don't take up any more memory. Dependency properties are a bit slower though, because of boxing to and fro object and looking up in the hashtable.

The Dependency Object framework furthermore allows for a lot of features like change notifications etc. I found a good resource that explains the inner workings here: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c33a8359-58be-430d-b949-cb6e6f32d8aa

I agree the syntax to declare them is a bit ugly, but you can create helpers to alleviate that a bit.

Upvotes: 3

Morten Mertner
Morten Mertner

Reputation: 9474

What you're looking for is chapter 3 of WPF Unleashed. It's a free sample chapter.

Upvotes: 1

Tim Ridgely
Tim Ridgely

Reputation: 2420

This is probably the most straightforward article describing DPs:

http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-dependency-properties-in-wpf/

Personally, I use them most often when I need to expose properties so that they can be databound via XAML. For example, if I make a UserControl that I want to use in XAML, and I want to set a property on the UserControl via XAML, I expose it as a dependency property.

Upvotes: 1

Greg Bogumil
Greg Bogumil

Reputation: 1923

the best use I saw for it is for attaching properties to classes which you cannot modify. So if you get a third party assembly you can attach extra information to the classes and read them when you need to.

Upvotes: 0

Related Questions