What is a Dependcney Property
Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a common language runtime (CLR) property. Collectively, these services are typically referred to as the WPF property system. A property that is backed by the WPF property system is known as a dependency property
Wait!! Why do we need to Extend the functionality of CLR propoperty?? What adwantage do we get?
Functionalities provided by a Dependency Property
- Data binding
- Metadata overrides
- Property value inheritance
- Reduced memory footprint
- WPF Designer integration
Dependency properties are pretty confusing if you try and understand them by looking at one. It kind of looks like a property so it's static... but you clearly can have a different value per button for a given dependency property.
The thing to understand is that there isn't just one value stored, these are associated with the button rather than held in a static. The static part is about accessing that data rather than holding it.
What you're looking at in that static is not a property. Ok, that's obvious.
Why is Dependency Properties Static
Each dependencyProperty object is registered in a global HashTable [Key + DP Name + Owner Type]
Each DependencyProperty object is created once for every DependencyProperty you define in your property system and not on every instance of object you create.
DependencyProperty maintains a static reference of all the DependencyProperty you register in WPF object hierarchy. It maintains a HashTable named PropertyFromName which it uses internally to get the DependencyProperty object.
Hence according to what we just seen, all TextBox will have only one TextProperty dependency property that go inside the field PropertyFromName.
It contains only the default metadata i.e. default values and callbacks(coerce, validate etc)
How is a Dependency Property Stored
- DependencyObject instance actually maintains a collection of objects (EffectiveValues) which will be set only when the instance stores / modifies the value of the DependencyProperty.
- The code gets either from the own collection or gets it from a method GetEffectiveValue.
- The GetEffectiveValue identifies whether the current value is in animation mode (DependencyProperty supports animation) or is an Expression.
How do styles impact the dependency property
Styles produce a separate entity that holds a Key Value collection of a DependencyProperty and its value, once it is applied to a FrameworkElement.
How is a Dependency Property Resolved
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.
- Coercion: Property system coercion
- Animation: Active animations, or animations with a Hold behavior
- Local Value:
- Template Properties: TemplatedParent template properties
- Implicit style: Applies only to the Style property. The Style property is filled by any style resource with a key that matches the type of that element. That style resource must exist either in the page or the application; lookup for an implicit style resource does not proceed into the themes.
- Style Trigger: Style triggers
- Template triggers
- Style setters
- Default Style Setter
- Inherited Value
- Default Value
Property that is declared by one control and attached to another. It allows inclusion of additional information by the control for later use.