22

Why a dependency property has to be Static?

I have seen that it has been already asked in some post here, but I am not able to understand it properly.

It will be great if someone can help me understand with a small snippet too.

Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • possible duplicate of [Non-static dependency properties](http://stackoverflow.com/questions/3660696/non-static-dependency-properties) – M.Babcock Oct 18 '13 at 04:34
  • The explanation in the answer for the above question is rather fundamental, please read up on the basics then we can debate whether the constraints are valid. – M.Babcock Oct 18 '13 at 04:36
  • @M.Babcock: That is what I have said in description that I have already seen few explanations but which are not comprehensive. I have already seen the link you gave before writing my question here, and, as well as I have also seen this http://stackoverflow.com/questions/2989431/why-are-dependency-properties-static – Jasmine Oct 18 '13 at 04:44
  • I will do possible read up on this while parallely waiting if someone can help me understand it in a better and comprehensive way. – Jasmine Oct 18 '13 at 04:45

2 Answers2

30

The magic here is, the declaration of DependencyProperty is static not its value (i.e the memory storage). The declaration that you add with static keyword is just the identifier(key) of the DependencyProperty for a particular DependencyObject. As same identifier/key can be used by all instances of the DependencyObject to identify the property value hence it makes sense to make it static.

On the other hand, when we set the value of DependancyProperty by calling the SetValue on DependancyObject instance, then each instance of DependancyObject on which the SetValue is called will store its local value of the Property. This is handled internally by the DependancyObject class which maintain sort of Dictionary which has the mapping between the DependancyProperty identifier and the local value.

Nitin
  • 18,344
  • 2
  • 36
  • 53
  • Thank you so much Nit, that helps me a lot. I am also looking for some good snippet where I can understand it better. – Jasmine Oct 18 '13 at 04:50
  • I don't understand this line - "Same identifier will be shared by all the instances of that Dependency Object" It will be great if you could please explain me, I am newbie to programming:( – Jasmine Oct 18 '13 at 05:01
  • 1
    you can take it as the key of the keyvaluepair of the Dictionary that DependancyObject has to store the value of dependency property. Since each instance can have different value of property but to identify that value we dont need different keys, because the value of each instance will corresponds to the same key.. hence it is static – Nitin Oct 18 '13 at 05:19
  • Thanks a lot Nit, that helps me understand it better. Cheers – Jasmine Oct 18 '13 at 05:35
  • 1
    glad it helped..if you want to read in detail on value resolution of dependency property.. here is a good visual representation http://wpftutorial.net/DependencyProperties.html – Nitin Oct 18 '13 at 05:38
  • I don't understand how you manage state inside the object if it is static. Can anyone explain that? – rollsch Sep 13 '17 at 00:50
8

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty. So default value for all instance of our class is same and system does not reserve memory for DependencyProperty on each and every instance of that class. This way it reduces the memory footprint.

Now the next question arise what if we explicitly set the DependencyProperty’s value for objects of the class.(By code or by animation or by style)

In this case DependencyObject comes into the picture. Any class which has DependencyProperty has to be derived from DependencyObject class (WPF specific class which maintains a collection named EffectiveValues). When user set the DependencyProperty’s value explicitly on the object of the class(By code or by animation or by style), the value is stored in the that EffectiveValues collection which resides within the DependencyObject class and reserve memory there.

  • Great answer; however, DependencyObject is not a WPF-specific class -- it's available in Silverlight and WinRT as well. – B.K. Dec 20 '15 at 21:13