Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Dependency property are declared as static readonly?

It is clear to me why dependency property are static and the question still remain on my mind is why we need to use Readonly keyword at the time of declaration of Dependency Property.

like image 964
pchajer Avatar asked Apr 10 '11 05:04

pchajer


People also ask

Why are dependency properties static?

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.

Why dependency property is readonly in WPF?

Existing read-only dependency properties For example, the Windows Presentation Foundation (WPF) framework implements the IsMouseOver property as read-only because its value should only be determined by mouse input. If IsMouseOver allowed other inputs, its value might become inconsistent with mouse input.

What is correct answer about the dependency property in WPF?

Dependency properties and the WPF property system extend property functionality by providing a type that backs a property, as an alternative to the standard pattern of backing a property with a private field. The name of this type is DependencyProperty.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.


2 Answers

Conceptually a dependency property is something that a dependency object simply has and that does not depend on when you use the property. Just like a CLR property, if you ask does this object have a Total property, you know it cannot be a double now but an int later. As a result, we'd make the dependency property const if we could, but we cannot, so readonly is the next best thing.

Using the readonly keyword has at least three effects:

  • it informs readers of the code that the value will not change
  • it prevents the author from accidentally changing the value
  • it assists the compiler, which benefits from knowing when things will not change
like image 57
Rick Sladkey Avatar answered Oct 18 '22 03:10

Rick Sladkey


Because it makes it obvious, that the value of this property cannot be changed after initialization.

like image 24
Euphoric Avatar answered Oct 18 '22 04:10

Euphoric