Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Dependency Properties

Introduction

Value resolution strategy

The magic behind it

How to create a DepdencyProperty

Readonly DependencyProperties

Attached DependencyProperties

Listen to dependency property changes

How to clear a local value

Introduction

When you begin to develop appliations with WPF, you will soon stumble across DependencyProperties. They look quite similar to normal .NET properties, but the concept behind is much more complex and powerful.

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

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

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

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

Value resolution strategy

Every time you access a dependency property, it internally resolves the value by following the precedence from high to low. It checks if a local value is available, if not if a custom style trigger is active,... and continues until it founds a value. At last the default value is always available.

The magic behind it

Each WPF control registers a set of DependencyProperties to the static DependencyProperty class. Each of them consists of a key - that must be unique per type - and a metadata that contain callbacks and a default value.

All types that want to use DependencyProperties must derive from DependencyObject. This baseclass defines a key, value dictionary that contains local values of dependency properties. The key of an entry is the key defined with the dependency property.
When you access a dependency property over its .NET property wrapper, it internally calls GetValue(DependencyProperty) to access the value. This method resolves the value by using a value resolution strategy that is explained in detail below. If a local value is available, it reads it directly from the dictionary. If no value is set if goes up the logical tree and searches for an inherited value. If no value is found it takes the default value defined in the property metadata. This sequence is a bit simplified, but it shows the main concept.

How to create a DependencyProperty

To create a DependencyProperty, add a static field of type DepdencyProperty to your type and call DependencyProperty.Register() to create an instance of a dependency property. The name of the DependendyProperty must always end with ...Property. This is a naming convention in WPF.

To make it accessable as a normal .NET property you need to add a property wrapper. This wrapper does nothing else than internally getting and setting the value by using the GetValue() and SetValue() Methods inherited from DependencyObject and passing the DependencyProperty as key.

Important: Do not add any logic to these properties, because they are only called when you set the property from code. If you set the property from XAML the SetValue() method is called directly.

If you are using Visual Studio, you can type propdp and hit 2x tab to create a dependency property.

 
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty = 
     DependencyProperty.Register( "CurrentTime", typeof(DateTime),
     typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));
 
// .NET Property wrapper
public DateTime CurrentTime
{
    get { return (DateTime)GetValue(CurrentTimeProperty); }
    set { SetValue(CurrentTimeProperty, value); }
}
 
 

Each DependencyProperty provides callbacks for change notification, value coercion and validation. These callbacks are registered on the dependency property.

 
new FrameworkPropertyMetadata( DateTime.Now, 
                       OnCurrentTimePropertyChanged, 
                       OnCoerceCurrentTimeProperty ),
                       OnValidateCurrentTimeProperty );
 
 

Value Changed Callback

The change notification callback is a static method, that is called everytime when the value of the TimeProperty changes. The new value is passed in the EventArgs, the object on which the value changed is passed as the source.

 
private static void OnCurrentTimePropertyChanged(DependencyObject source, 
        DependencyPropertyChangedEventArgs e)
{
    MyClockControl control = source as MyClockControl;
    DateTime time = (DateTime)e.NewValue;
    // Put some update logic here...
}
 
 

Coerce Value Callback

The coerce callback allows you to adjust the value if its outside the boundaries without throwing an exception. A good example is a progress bar with a Value set below the Minimum or above the Maximum. In this case we can coerce the value within the allowed boundaries. In the following example we limit the time to be in the past.

 
private static object OnCoerceTimeProperty( DependencyObject sender, object data )
{
    if ((DateTime)data > DateTime.Now )
    {
        data = DateTime.Now;
    }
    return data;
}
 
 

Validation Callback

In the validate callback you check if the set value is valid. If you return false, an ArgumentException will be thrown. In our example demand, that the data is an instance of a DateTime.

 
private static bool OnValidateTimeProperty(object data)
{
    return data is DateTime;
}
 
 

Readonly DependencyProperties

Some dependency property of WPF controls are readonly. They are often used to report the state of a control, like the IsMouseOver property. Is does not make sense to provide a setter for this value.

Maybe you ask yourself, why not just use a normal .NET property? One important reason is that you cannot set triggers on normal .NET propeties.

Creating a read only property is similar to creating a regular DependencyProperty. Instead of calling DependencyProperty.Register() you call DependencyProperty.RegisterReadonly(). This returns you a DependencyPropertyKey. This key should be stored in a private or protected static readonly field of your class. The key gives you access to set the value from within your class and use it like a normal dependency property.

Second thing to do is registering a public dependency property that is assigned to DependencyPropertyKey.DependencyProperty. This property is the readonly property that can be accessed from external.


 
// Register the private key to set the value
private static readonly DependencyPropertyKey IsMouseOverPropertyKey = 
      DependencyProperty.RegisterReadOnly("IsMouseOver", 
      typeof(bool), typeof(MyClass), 
      new FrameworkPropertyMetadata(false));
 
// Register the public property to get the value
public static readonly DependencyProperty IsMouseoverProperty = 
      IsMouseOverPropertyKey.DependencyProperty;    
 
// .NET Property wrapper
public int IsMouseOver
{
   get { return (bool)GetValue(IsMouseoverProperty); }
   private set { SetValue(IsMouseOverPropertyKey, value); }
}
 
 

Attached Properties

Attached properties are a special kind of DependencyProperties. They allow you to attach a value to an object that does not know anything about this value.

A good example for this concept are layout panels. Each layout panel needs different data to align its child elements. The Canvas needs Top and Left, The DockPanel needs Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's not possible to have all those properties on all WPF controls.

The solution are attached properties. They are defined by the control that needs the data from another control in a specific context. For example an element that is aligned by a parent layout panel.

To set the value of an attached property, add an attribute in XAML with a prefix of the element that provides the attached property. To set the the Canvas.Top and Canvas.Left property of a button aligned within a Canvas panel, you write it like this:

<Canvas>
    <Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
</Canvas>
 
 
public static readonly DependencyProperty TopProperty =
    DependencyProperty.RegisterAttached("Top", 
    typeof(double), typeof(Canvas),
    new FrameworkPropertyMetadata(0d,
        FrameworkPropertyMetadataOptions.Inherits));
 
public static void SetTop(UIElement element, double value)
{
    element.SetValue(TopProperty, value);
}
 
public static double GetTop(UIElement element)
{
    return (double)element.GetValue(TopProperty);
}
 
 

Listen to dependency property changes

If you want to listen to changes of a dependency property, you can subclass the type that defines the property and override the property metadata and pass an PropertyChangedCallback. But an much easier way is to get the DependencyPropertyDescriptor and hookup a callback by calling AddValueChanged()

 
DependencyPropertyDescriptor textDescr = DependencyPropertyDescriptor.
    FromProperty(TextBox.TextProperty, typeof(TextBox));
 
if (textDescr!= null)
{
    textDescr.AddValueChanged(myTextBox, delegate
    {
        // Add your propery changed logic here...
    });
} 
 
 

How to clear a local value

Because null is also a valid local value, there is the constant DependencyProperty.UnsetValue that describes an unset value.

button1.ClearValue( Button.ContentProperty );
 




Last modified: 2010-02-08 17:52:30
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Mukesh Rawat
Commented on 7.May 2009
Great stuff, keep it up man.
pandu
Commented on 11.May 2009
good keep it up
Rahul Kumar
Commented on 19.May 2009
"If you set the property from XAML the GetValue() method is called directly" should read "If you set the property from XAML the SetValue() method is called directly"?
Christian Moser
Commented on 19.May 2009
Hi Rahul,
thanks for the feedback. I corrected it.
Mena Saad
Commented on 29.May 2009
Thanks for the great articles ,they really helped me
sarada
Commented on 29.May 2009
Explanation is really good. It gave me better understanding about the dependency properties. Thank you.
Andre Pena
Commented on 31.May 2009
Thank you so much for this valuable resource
webchetan
Commented on 9.June 2009
Great Stuff man! Excellent explaination! I have bookmarked your website!
daya
Commented on 11.June 2009
Good one... but confusing.
Benoit
Commented on 11.June 2009
Do you know a way for a control to be created with DateTime.Now?
--> The problem is all controls will have the same "now" value, has it is a static property.
My goal is not to create a clock, but a search form, so I want it to be set at now at the creationg, but I don't want it to follow the clock.
Christian Moser
Commented on 13.June 2009
Hi Benoit,
maybe it could be a solution to set the Date property to DateTime.Now in the constructor if the object.
anonimo
Commented on 20.June 2009
.ClearValue( <------ great
psychowico
Commented on 28.June 2009
In one of code bloks you thing about "public bool IsMouseOver", not "public int IsMouseOver", yes?
You tutorials are great, keep it up ;)
psychowico
Commented on 28.June 2009
In one of code bloks you thing about "public bool IsMouseOver", not "public int IsMouseOver", yes?
You tutorials are great, keep it up ;)
sam
Commented on 1.July 2009
very nice
micaleel
Commented on 4.July 2009
Impressively elaborate and simple to grok. Thanks
Jay
Commented on 18.July 2009
Cool Stuff man.
Yaz
Commented on 19.July 2009
Nice article, some code download would be nice
dantarion
Commented on 23.July 2009
DependendyProperty <-typo
Farzad
Commented on 28.July 2009
Fantastic
Thanks a lot !
Syed
Commented on 29.July 2009
What if a developer create a same dependency property which is already exists? How the Dependency Property System resolves this problem?
Joel Zeal
Commented on 10.August 2009
Good stuff...keep it flowing.
Alex
Commented on 12.August 2009
This is the best explanation i have found
Steve Horsfield
Commented on 13.August 2009
I have added an example of how to create dependency properties in F# on my blog as there are some tricks to it. You can find the information here: http://stevehorsfield.wordpress.com/2009/08/13/creating-wpf-dependency-properties-in-fsharp/
Ryan
Commented on 17.September 2009
One typo error in the figure 2, the word "interited" should be "Inherited".
Jesse
Commented on 5.October 2009
This is a well-written article but these typos are inexcusable. A 1st grader could run a spell check and catch 99% of the errors on this page
Farrukh
Commented on 8.October 2009
really good tutorial for beginner
Sanjay
Commented on 13.October 2009
Helped me in understanding WPF !!! waiting for the remaining chapters.. Thanks
Hisham Safwat
Commented on 22.October 2009
thank you very good tutorial hope u continue for more about WPF
Sreeroop
Commented on 22.October 2009
Very good...
Give a brief exmple
Suhas Kulkarni
Commented on 27.October 2009
nice one...! Really this will help me alot to understand Properties...
Thnx:)
Developer
Commented on 27.October 2009
Nice tutorial
Shekar
Commented on 10.November 2009
WPF personified
melvin
Commented on 1.December 2009
WOw... Where'd you get all this stuffs? I have been nosebleeding.. :) I can't really get the idea.. too difficult.. heheh.. But thanks anyway.. very nice presentation.. Great job...
Mr. X
Commented on 7.December 2009
Niccccccccccccccccccccccccccccccccccccccccccccccce
Soumyajit
Commented on 21.December 2009
Best Tutorial man... please post more
chbanello
Commented on 21.December 2009
Great post, thanks.

I have a question: which one validation callback or coerce value fires first?
chbanello
Commented on 22.December 2009
Great post, thanks.

I have a question: which one validation callback or coerce value fires first?
Ch Srinivas...
Commented on 24.December 2009
It is really excellent way of describing the topic.Thanks
rukawa110
Commented on 27.December 2009
Too many typos, but in the end, it beats the heck out of those incomprehensible heavy textbook tutorials out there anyday. Very nice job!!!! Hope you will keep putting up more nice stuff!!
Satya Dev...
Commented on 1.January 2010
Hi Here is my understanding about dependency properties:

1. The main difference between a normal CLR/.Net property and a WPF dependency property is the location where the internal value of the property is stored

2. In a normal CLR/.Net property case the internal value that is accessed by a property getter and setter methods is most of the time defined as a private field variable either in the same class that the property is defined accessed from or in one of its base classes

3. In the case of a dependency property the actual value that is accessed by a property getter and setter methods is NOT a direct field variable either in the same class on which the property is accessed from or NEITHER a direct field variable in one of its base classes.

This means in a dependency property case the actual Property name and the value that the property gets and sets for the callers are loosely coupled.
Yannick
Commented on 19.January 2010
Very helpful. Thank you
Thoại...
Commented on 20.January 2010
Thanks, your post is the easiest article to understand I've ever read about DP and DO
elle
Commented on 21.January 2010
cool tut! Just one thing..On the Readonly DependencyProperties sample part, the return type should be bool and not int :P
Nasim
Commented on 25.January 2010
Very Nice.Very helpful. Thanks a lot !
Rathi
Commented on 28.January 2010
Hi very nice and helpful.......
Aunt KK
Commented on 31.January 2010
Great stuff. Thank you.
Craig
Commented on 3.February 2010
Great stuff, but needlessly complex.
xiaoma
Commented on 3.February 2010
Great tutorial. Thanks!
www.floatingvectors.com
Pavan
Commented on 4.February 2010
From past few days I was searching for good articles on Dependancy and Attached Properties. Only this article gave me damn clear explanation. Thanks a ton!
liyo
Commented on 8.February 2010
Great and helpful. Thanks!
Rohit
Commented on 9.February 2010
Great Article
Suresh
Commented on 18.February 2010
Wow!! Great article
Madhukar
Commented on 22.February 2010
Very usefull...Thanks!
Brij
Commented on 25.February 2010
Too Gooooooooooooooooood!!!!!!!
HaiBang
Commented on 4.March 2010
Great Job !
Nitin
Commented on 5.March 2010
very helpful !!
AC Milan
Commented on 6.March 2010
love it :) :)
Sundar Rajan
Commented on 10.March 2010
It's very much useful. Thanks a lot on behals of every one who has read this article and found useful .
Anders
Commented on 16.March 2010
Great article.
priya
Commented on 18.March 2010
good one...
Tumdik
Commented on 19.March 2010
What a awesome fuckin tutorial is this !!!!
Ameet
Commented on 23.March 2010
Good Explanation
HaydnSimon
Commented on 24.March 2010
Excellent. Your introduction to dependency properties is by far the best I have read. I would appreciate a little more technical detail on attached properties.
Rajapadmanab...
Commented on 29.March 2010
Crisp and clear explanations. Thanks
Manuel
Commented on 2.April 2010
Concise and clear. Is it too difficult for MSDN writers to explain things that plainly?
Moe
Commented on 5.April 2010
very good one, man now i have a clue what dependency property is!
www.vestus.com
ContraDick
Commented on 5.April 2010
Better entry level tutorial on dps here:
http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties
kevin
Commented on 5.April 2010
excellent tutorial. keep up the good work!!!
Ranjan
Commented on 7.April 2010
very very useful to me.....
Saurabh
Commented on 7.April 2010
Perfect explaination.. Have been searching for this for long.
Raman
Commented on 14.April 2010
Excellent,nice i am learning...
Jatin
Commented on 21.April 2010
Always Good for the beginners. Thanks
Ю
Commented on 22.April 2010
Тут был Юра
Jijo
Commented on 23.April 2010
Excellent article!
himanshu jani
Commented on 29.April 2010
Great Chritian..thanks a lot
Dany
Commented on 1.May 2010
I've read many articles but only this one explain me what dependency property really is. Thanks a lot
Mariam...
Commented on 3.May 2010
Perfect article, exactly what I've been searching for. Thanks a lot.
Javed
Commented on 4.May 2010
Very Useful - Thank you.
Shaun
Commented on 5.May 2010
Many thanks! What a great, to-the-point explanation.
Jaswant
Commented on 6.May 2010
Love it,to the point article:)
Sandeep
Commented on 9.May 2010
It's So nice explanation.....
I have cleared my so many doubts after reading this.....
Dpak
Commented on 17.May 2010
Great Article!!!!
Vinod Kushwaha
Commented on 17.May 2010
Great !!!!!!!!!!!!!!!!
Rohit Kandhal
Commented on 20.May 2010
public static readonly DependencyProperty TextBoxLostFocusCommand = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(TextBox.LostFocusEvent, "TextBoxLostFocusCommand", typeof(ControlBehaviour)); also one more thing related to dependency property.
Barun Prasad
Commented on 20.May 2010
Good one...
Prabhat
Commented on 26.May 2010
It's a nice article on dependency property.
Many Thanks !!!
Prabhat
Commented on 26.May 2010
It's a nice article on dependency property.
Many Thanks !!!
Tejas Patel
Commented on 7.June 2010
Thanks Dear. You really doing great Job by Helping others. May God Helps you when you need it. Thank you very much.
Ash
Commented on 22.June 2010
Good explanation. Perhaps it would be nice to mention a practical real-life example where dependency properties should be used as opposed to 'normal' properties. Not one from existing .NET controls or classes but when used in a class that we have to write
Emad
Commented on 27.June 2010
thank you very much 4 ur efforts.
I was searching for something simple and clear like ur post..
many thanks again :)
Emad
Commented on 27.June 2010
thank you very much 4 ur efforts.
I was searching for something simple and clear like ur post..
many thanks again :)
Sonam
Commented on 28.June 2010
Gud article.................
Thanks:)
Kiran
Commented on 29.June 2010
Very nice.
To learn a technology is primarily to know the limitations and capabilities, special features in it.These articles are really good in that aspect.
Thanks
lensso
Commented on 2.July 2010
太长了
Dejan
Commented on 6.July 2010
I like it but it is not simple. I cannot swallow it from once.
KWGWork.
tauqir
Commented on 6.July 2010
One of the Finest Explanation..... ever
Rehan
Commented on 8.July 2010
It is owsome & erotic
John
Commented on 8.July 2010
Great post. thanks puddy
Sachin
Commented on 8.July 2010
Nice article. To the point.
Shrichand Gupta
Commented on 14.July 2010
Hats off
Jo
Commented on 14.July 2010
Is there any way to deal with fields of an object changing issuing a dependency property change? I was using two DP's, one for X offset and other for Y, to make it a bit more uniform I converted to use a Point(I custom Point using a class instead of a struct). But if I change the internal field of the Point it shouldn't trigger a dependency change(it affectsrender) since the property didn't change?
Viplove Sharma
Commented on 16.July 2010
I did not encounter a more comprehensive explanation of a DependencyProperty. You simply rock!
Iyappan Mani
Commented on 24.July 2010
Very nice tutorial .. Very good..
Atul Kaushik
Commented on 29.July 2010
Fundoo
Andriy Buday
Commented on 2.August 2010
Great post. Article which gave me basic understanding of DP. Thanks.
Manoj Acharya
Commented on 4.August 2010
very nice article...
bindu
Commented on 5.August 2010
nice articele and explanation is also very good....but please provide a working example so as we can get a clear picture of how to use it in real time impplementations...please provide an exmaple that describes all the features and options available in an example so that its clear in knowing the logiacl flow and implementation....i am totally new to this concept so please help me in learning better
srini
Commented on 6.August 2010
Good one
dogsolitude_uk
Commented on 10.August 2010
Just started learning WPF and Dependency Properties really have me stumped. So far, a property has been something tangible, and attached to a particular object (may be a control, or an instance of a class you've programmed yourself).

This is going to take a bit of reading and re-reading, and perhaps a worked example showing firstly why a 'normal' property won't work in a given situation, and how one would resolve it using a dependency property.

I'll keep plugging away, but this site has become an invaluable learning resource! Thanks :)
Aravinthan A
Commented on 11.August 2010
Excellent!! Explanations make perfect sense.. Thank you!
KITHSIRI...
Commented on 13.August 2010
in the example..

new FrameworkPropertyMetadata( DateTime.Now,
OnCurrentTimePropertyChanged,
OnCoerceCurrentTimeProperty ),
OnValidateCurrentTimeProperty );

As last para is boolean , we have to call like:

new FrameworkPropertyMetadata( DateTime.Now,
(FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
OnCurrentTimePropertyChanged,
OnCoerceCurrentTimeProperty ,
OnValidateCurrentTimeProperty(DateTime.Now));

Sumaiya
Commented on 16.August 2010
Good Article.Thanks!!
Hmmm
Commented on 18.August 2010
Hmmm htet ge
Prajwal
Commented on 18.August 2010
Good one. Articles on WPF and WCF are very rare to find on net and what u r doing is great, so that one can atleaset begin working on it.
Samar
Commented on 19.August 2010
I hv a small question here. U hv wrote that the dependency property will only get updated through code but not through xaml. What do i need to change to make that work in xaml?? I hv a user control with a textbox in it. I hv a text property which is a dependency property and i hv done everything what u hv written above. When I change the text property through code (through a seperate button click) the observablecollection is getting updated but when i type the same change it is not getting updated. Do i need to add anything other than what u hv explained above??
ali.aghdam
Commented on 1.September 2010
Good Job.Thanks!
wpfbeginner
Commented on 3.September 2010
thanks, verry good. most of the german articles sucks...
Ajay
Commented on 7.September 2010
Nice article , explanation best part . Cheers :)
rahan
Commented on 8.September 2010
Nice one dear....but some thing missing
tavish
Commented on 17.September 2010
A Bit Complicated
Alice
Commented on 20.September 2010
People who can explain things like you do, usually suffer in life. Thanks anyway
Alice
Commented on 20.September 2010
People who can explain things like you do, usually suffer in life. Thanks anyway
Shivani
Commented on 20.September 2010
One of the best articles on DP. Thanks!
David
Commented on 22.September 2010
An informative overview which I have used to get fairly well acquainted with DPs. I have a further question more directly regarding inheritance:

Supposing I have a class which defines and registers a DP in the usual way, eg:

public static readonly DependencyProperty TintProperty =
DependencyProperty.Register(
"Tint",
typeof(Color),
typeof(MyClass),
new PropertyMetadata(Color.White));
--
public Color Tint
{
get { return (Color)this.GetValue(TintProperty); }
set { this.SetValue(TintProperty, value); }
}

Now supposing I then want to derive a second class "MyDerivedClass"
from "MyClass" with a new DP, say "Hue", which is essentially just a
clone of "Tint". I want it to get/set the value of Tint in the base
class so all code in the "MyClass" continues to work fine. But I want
to use <Setter Property="Hue" Value="Blue"> in XAML for "MyDerivedClass"

Clearly I have to register "Hue" with "MyDerivedClass". I therefore thought
that TintProperty.AddOwner(..) might work. But an XamlParserException with
the message "Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'" is always thrown on the setter.

Have you any suggetsions on reusing DPs in this way?
Harvey
Commented on 27.September 2010
I want to dress up like a nurse.
Rakesh
Commented on 30.September 2010
one question I needed to ask was is this possible that we declare non static DP.
Vivek Mehta
Commented on 6.October 2010
You mentiond Value Changed callback and then you described the listener for listening to dependency property changes. Can you please briefly state what is the difference between these two?
Shalini
Commented on 8.October 2010
very useful one !!!
Shafqat Ali
Commented on 12.October 2010
Greate Artical.....Please keep it up.
Andreas
Commented on 20.October 2010
Liked it! I already worked with DP, but now I have better understanding of the things I was doing...
John
Commented on 21.October 2010
Thanks! It was very clear and useful
Soby Mathew
Commented on 21.October 2010
Good Article.Thanks!!
Abhishek...
Commented on 21.October 2010
Thanks Christian.Its a superb article.
Steven Jeuris
Commented on 23.October 2010
Thanks a lot for this article! I just wrote a post about a factory class which may be used to simplify creating dependency properties. This article is referenced as an example. I'd love to hear your opinion about it since you clearly master dependency properties. :)

http://whathecode.wordpress.com/2010/10/23/dependency-property-factory/
Ashish...
Commented on 23.October 2010
Excellent explanation ... I was looking 4 the same comparison for a long

thanks a lot..
villa
Commented on 26.October 2010
Sounds like Css goodies with javascript tricks...Nice!
Muneeb
Commented on 28.October 2010
A bit complicated ...
Muneeb
Commented on 28.October 2010
Can you please explain "Dependency Properties" in a very simple terms ...
XXX
Commented on 29.October 2010
Good job!
Azhar
Commented on 31.October 2010
Best Article on DP.
Imran
Commented on 3.November 2010
Good
Julien
Commented on 11.November 2010
Superb! Thanks for the help, appreciated. Complex stuff made simpler!
Paganel90
Commented on 11.November 2010
Good one! Thanks! but i've got a question about Validation Callback delegate...i can't find any overloads of the FrameworkPropertyMetadata's contstructor that accepts this kind of delegate! this is the full new() overloads....
public FrameworkPropertyMetadata(object defaultValue, System.Windows.FrameworkPropertyMetadataOptions flags, System.Windows.PropertyChangedCallback propertyChangedCallback, System.Windows.CoerceValueCallback coerceValueCallback, bool isAnimationProhibited, System.Windows.Data.UpdateSourceTrigger defaultUpdateSourceTrigger)....
is this real?
Commented on 15.November 2010
Is it?
Kim
Commented on 16.November 2010
Probably worth mentioning that System.Windows.Controls.Canvas already has a static GetTop and thus if you try to follow this example directly, you'll get binding errors at runtime.

http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas_methods.aspx
Kim
Commented on 17.November 2010
Probably worth mentioning that System.Windows.Controls.Canvas already has a static GetTop and thus if you try to follow this example directly, you'll get binding errors at runtime.

http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas_methods.aspx
uninda
Commented on 23.November 2010
gooooooooooooooooooooood
deva
Commented on 24.November 2010
Great.... very useful
Piekop
Commented on 26.November 2010
Excellent! Nice work!
Srinivasa
Commented on 2.December 2010
Useful information for learners.
Seamus
Commented on 19.December 2010
I've red WPF Unleashed, a WPF Custom Control book, and others. This is the best and most succinct explanation of DP's I've seen.
muthu
Commented on 8.January 2011
hi
Brad
Commented on 11.January 2011
The other use of Dependency Properties vs. CLR properties is that DPs don't use reflection and are therefore faster.
Peer
Commented on 12.January 2011
Nice article...thanks for that...however I have a question on the section "Listen to dependency property changes": It is common practice to register dependency properties in the static constructor of a control...if doing so, how can I access an attribute resp. instance variables of the control class such as "myTextBox" as stated in your given example ? As instance variables are not accessible from within a static constructor this brings up the question if one has to deviate from this common practice and do it all in the instance ctor ? Thanks for your time and consideration...
Uday...
Commented on 13.January 2011
Very nice article about Dependency Properyy
Samu
Commented on 17.January 2011
Can you please clear my doubts?

Is it always required to define a DependencyProperty as static? Why?
If I define a collection type DependencyProperty then the property become a static collection for all objects!

public class ElementsCollection : ObservableCollection<Element> { }

public static readonly DependencyProperty ElementsProperty = DependencyProperty.Register
("Elements",
typeof(ElementsCollection),
typeof(Title),
new PropertyMetadata(new ElementsCollection(), OnElementsPropertyChanged));
sdf
Commented on 20.January 2011
fine
Rags
Commented on 24.January 2011
Very Nice one, especially the magic behind of Dependancy property section.
Rags
Commented on 24.January 2011
Very Nice one, especially the magic behind of Dependancy property section.
Tharindu
Commented on 25.January 2011
Nice article. Small error in the code sample in ReadonlyDependancyProperty .NET wrapper. It should be;
public bool IsMouseOver. Not int.
Cheers.
dmedellin
Commented on 4.February 2011
thanks a lot, very usefull, now i understand a little bit more
praveen kumar
Commented on 7.February 2011
ye kya pagal pan hai
dinu
Commented on 16.February 2011
what is MyClockControl
Viswa
Commented on 21.February 2011
Romba nalla iruku
anseer
Commented on 23.February 2011
gud
great always
Commented on 5.March 2011
damn good buddy
Igor
Commented on 27.March 2011
Very good! Could you please cover attached properties in the same manner?
I'm now digging through MSDN and it seems that attached ones are not necessarily dependency props.

MSDN is quite confusing, by the way. After reading through tons of articles, I had to use google to check if dependency properties are just static dictionaries of wrappers after all. Found this article and it seems that they are.
khizar
Commented on 25.April 2011
Good tutorial with useful examples.
Shashank
Commented on 28.April 2011
Awesome
izod
Commented on 28.April 2011
Excellent. I've been digging for a clear explanation of DependencyProperty and this is th best explation. Thank you.
harish chandna
Commented on 12.May 2011
very nice, useful for me.
harish chandna
Commented on 12.May 2011
very nice, useful for me.
Rahul Bhojane
Commented on 15.May 2011
good
Volodymyr S.
Commented on 17.May 2011
Good job, cap!!!! please continue - very very useful
Ali
Commented on 27.May 2011
Very Goooddddd !!
Thanks.
Fadi Raheel
Commented on 4.June 2011
Very good explanation of DependencyProperty
Adriano
Commented on 9.June 2011
Could you please put a XAML sample of the binding to the property developed in the code? Thanks.
Jay
Commented on 12.June 2011
The only article that does not confuse readers on Dependency Properties
Hal
Commented on 17.June 2011
Nice attempt at an explanation of DependencyProperty. It could be better organized.
Rene
Commented on 20.June 2011
YES !
Satyabrata
Commented on 24.June 2011
Good, very nice article. Thanks
Stefan
Commented on 5.July 2011
Thank you for spending the time to prepare this article. I have already read the MSDN articles on dependency properties as well as Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel (Apress 2010), so understanding your article was easy for me. What is omitted from all of these is a lowest common denominator fully working example that is useful (or at least can be extended to be useful). MSDN uses a silly fish tank example. Provide a second example that builds on the first in order to illustrate the advanced concepts like callbacks.
Aymen
Commented on 17.July 2011
Thanks thanks for this wonderful tutorial it really helped me a lot to understand DependencyProperties , I just still have a question , I don't understand the fact that the dependency property is declared as static so how comes that every instance has its own value this is the only misundestanding I still have I need your help , thanks in advance .
mahanth
Commented on 18.July 2011
Thank you very much.i was looking like this type of article
Tronel
Commented on 21.July 2011
Very useful for me. Thanks
balaji
Commented on 28.July 2011
very nice and useful
daanyaal
Commented on 2.August 2011
You will need to read this about 10 times to Understand what is going on. I understand a little bit now about DP, much better than msnd
Xus
Commented on 3.August 2011
Awesome! Very well explained.
Maya
Commented on 4.August 2011
Very nice explanation on dependency properties............finally understood the concept ! Thanks for posting this.
Ankush Gupta
Commented on 8.August 2011
Good.. Nice Article
Cryptic
Commented on 15.August 2011
I m the beginner of WPF. wow! thts really great to have site like this. Thanks. its helping me a lot.
blah!!!blah!!!
Commented on 19.August 2011
why is the dependency property declared as readonly if you are also using the SetValue method
Rithuu Mangala
Commented on 25.August 2011
Content is Precise and well defined. Thanks for writing this article.
Nahum
Commented on 28.August 2011
Thanks! much clearer than many articles.
seyed
Commented on 6.September 2011
Thank you a lot :)
There is an miistake on Readonly DependencyProperties section. Wrapper method is defined as 'int' but DependencyProperty is already as 'bool'.
Also, i couldn't use 'Listen to dependency property changes' section.
Can you explain some more info about it? Where i must insert these codes?

Thank's again :)
Mayank Gupta
Commented on 16.September 2011
Very Nice Post man
Ohad
Commented on 22.September 2011
Hi Christian
Thanks for your detailed explanation!
I have few questions :
1. At the begining when you expalnied about the advantages one of them was &quot;Reduced memory footprint&quot; u wrote that when using DP it reduces memory storage compare to Normal Properties because it &quot;only stores modified properties in the instance &amp; The default values are stored once within the dependency property&quot;.

I understand that when storring only changes it reduces memory storage but what does it mean that the default values are &quot;stored within the dependency property&quot;? i mean they must be stored somewhere when application runs, so where would that be?
so why DP takes less memory than Normal Property?

2.You wrote that DP are stored in Dictionary instaed of Field like Normal properties.
i wanted to know how is this fact empowers DP compared to normal Prop'. why is it
better?

3.I understood that there are &quot;Readonly DP&quot; but i have noticed that when using the shortcutin Visual Studio for creating regular DP (propdp) a
&quot; public static readonly DependencyProperty MyProperty&quot; is added.
so what is the difference when adding &quot;readonly&quot; statement in regular DP and ReadOnly DP?

4. Maybe a silly question but what is the meaning of &quot;Dependency&quot; in Dependency
Properties?

5. Why is the dependency property must be defined as static?
ohad
Commented on 25.September 2011
Hi Christian
Thanks for your detailed explanation!
I have few questions :
1. At the begining when you expalnied about the advantages one of them was &quot;Reduced memory footprint&quot; u wrote that when using DP it reduces memory storage compare to Normal Properties because it &quot;only stores modified properties in the instance &amp; The default values are stored once within the dependency property&quot;.

I understand that when storring only changes it reduces memory storage but what does it mean that the default values are &quot;stored within the dependency property&quot;? i mean they must be stored somewhere when application runs, so where would that be?
so why DP takes less memory than Normal Property?

2.You wrote that DP are stored in Dictionary instaed of Field like Normal properties.
i wanted to know how is this fact empowers DP compared to normal Prop'. why is it
better?

3.I understood that there are &quot;Readonly DP&quot; but i have noticed that when using the shortcutin Visual Studio for creating regular DP (propdp) a
&quot; public static readonly DependencyProperty MyProperty&quot; is added.
so what is the difference when adding &quot;readonly&quot; statement in regular DP and ReadOnly DP?

4. Maybe a silly question but what is the meaning of &quot;Dependency&quot; in Dependency
Properties?

5. Why DP are always declared as Static?

6. why when updating data from Model to View we have to use DP or NotiftyPropertyChange instead of normal property,

and when updating data from View to Model we have to use Normal Property?

dsd
Commented on 25.September 2011
hello

Name
E-Mail (optional)
Comment