15

Could someone please explain why dependency properties are declared as static ?

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
Kishore Kumar
  • 21,449
  • 13
  • 81
  • 113

2 Answers2

14

When you declare a DependencyProperty, you are declaring the definition of that property, rather than the storage space for the property's value (as would be the case with the regular property declaration). There is only one definition of the property for the whole class - as opposed to one definition per instance of the class - and so it is static.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 6
    Any object with a `DependencyProperty` derives from type `DependencyObject`. An instance of `DependencyObject` includes a property bag - essentially a dictionary of property name mapped to value - and the value of any `DependencyProperty` that is set is stored in that bag. – Dan Puzey Jun 21 '13 at 10:30
12

The field you declare as static is only the identifier of a dependency property, not the value of the property. It is shared across all instances of the class, and is used to get/set the value of the property for each instance.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I have some requirement of making the DP as static because i want to shared it across all instances. Is it possible? – Rohit Vats Jun 30 '12 at 18:53
  • @RV1987, it doesn't really make sense... if you want to do that, don't use a DP, just use a static CLR property – Thomas Levesque Jul 08 '12 at 21:41
  • @ThomasLevesque: Seriously I cannot understand the meaning or justification of your statements what you are trying to say. I am newbie though and do not understand your words. Why we should set value for every instance if it is static. Static means its one time invokation throughout the program lifetime right. – Jasmine Oct 18 '13 at 05:08
  • 1
    @Divine, I'm not sure I understand your question... A dependency property is defined by an identifier field (e.g. `TextBox.TextProperty`), which is static. To get the value of the property for a given instance, you call `GetValue(TextProperty)` on the instance; to make things easier, most DPs have an instance property that acts as a shortcut (e.g. `TextBox.Text`) – Thomas Levesque Oct 18 '13 at 08:15
  • BTW, downvoting my answer just because you don't understand it not very useful... – Thomas Levesque Oct 18 '13 at 08:17
  • @ThomasLevesque: I up voted it again :) However it is saying, my vote is locked. I di not know what to do. Well I am afraid I didn't still understand anything. However, I will give some reading through some videos too. Thanks buddy for help. – Jasmine Oct 18 '13 at 16:44
  • Could you, please, explain also why dependency property is shared across all instances of the class? I can't imagine how it possibly can be useful. Isn't it encapsulation violation? – Dawid Aug 07 '14 at 09:16
  • @havekar, my answer already explains it... it's not the property that is static, it's the *identifier* of the property; each instance has a its own value, of course. Dependency properties are a mechanism completely different from "normal" CLR properties; they enable scenarios like binding, animations, etc. And it has nothing to do with encapsulation. – Thomas Levesque Aug 07 '14 at 12:29