7

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

developer
  • 5,178
  • 11
  • 47
  • 72
  • Partial duplicate: http://stackoverflow.com/questions/2505234/need-a-short-and-clear-definition-for-dependency-properties – itowlson Apr 02 '10 at 19:42

5 Answers5

13

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • So will it be something like, when the lastname changes from test1 to test2, change the font from green to yellow? Just an example.. – developer Apr 02 '10 at 19:35
  • Well - even just having last name display. Take the "TextBlock" control - the "Text" property is a dependency property, which is why you can do: – Reed Copsey Apr 02 '10 at 19:37
  • @developer: LastName can be a normal property on a class, but Text must be a DependencyProperty in order to be the binding "Target" - ie: the thing you're binding to in XAML. – Reed Copsey Apr 02 '10 at 19:38
  • Can you also explain me how is the value set. In the below example, public class Person : DependencyObject { public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(Person)); public string LastName { get { return (string)GetValue(LastNameProperty); } set { SetValue(LastNameProperty, value); } } } How is the value property set? and what will be the xaml that I shall be using. The above is a googled example. – developer Apr 02 '10 at 20:56
  • @developer: The "default" value can be specified in the metadata when you call Register - this is the value it will have if you never call set. The actual values get set by DependencyObject.SetValue(), which is why the CLR property needs to wrap that. – Reed Copsey Apr 02 '10 at 23:07
  • Amazingly simple explanation. Thanks! – Marshall Alsup Jun 01 '11 at 20:49
  • I like the explanation @Reed provided !! – Turbot May 14 '12 at 15:44
3

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.

herzmeister
  • 11,101
  • 2
  • 41
  • 51
1

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.

Tim Ridgely
  • 2,400
  • 1
  • 18
  • 25
1

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

Morten Mertner
  • 9,414
  • 4
  • 39
  • 56
0

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.

Greg Bogumil
  • 1,903
  • 15
  • 23