Thursday, July 31, 2014
Dependency Property Value Precedence
When you get the value of a dependency property, you are
potentially obtaining a value that was set on that property through any
one of the other property-based inputs that participate in the WPF
property system. Dependency property value precedence exists so that a
variety of scenarios for how properties obtain their values can interact
in a predictable way.
Consider the following example. The example includes a style that applies to all buttons and their Background properties, but then also specifies one button with a locally set Background value.
In principle, for the first button, the property is set
twice, but only one value applies: the value with the highest
precedence. A locally set value has the highest precedence (except for a
running animation, but no animation applies in this example) and thus
the locally set value is used instead of the style setter value for the
background on the first button. The second button has no local value
(and no other value with higher precedence than a style setter) and thus
the background in that button comes from the style setter.
Consider the following example. The example includes a style that applies to all buttons and their Background properties, but then also specifies one button with a locally set Background value.
|
|
|---|
| Note |
|---|
|
The SDK documentation uses the terms "local value" or "locally set value" occasionally when discussing dependency properties. A locally set value is a property value that is set directly on an object instance in code, or as an attribute on an element in XAML. |
<StackPanel> <StackPanel.Resources> <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}"> <Setter Property="Background" Value="Red"/> </Style> </StackPanel.Resources> <Button Background="Green">I am NOT red!</Button> <Button>I am styled red</Button> </StackPanel>
Thursday, July 24, 2014
DependencyProperties
- 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 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.
- 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.
Here is a very good article for DependancyProperty.
http://wpftutorial.net/DependencyProperties.html
http://wpftutorial.net/DependencyProperties.html
DependencyProperty.Register
DependencyProperty.RegisterReadOnly
DependencyProperty.RegisterAttched
Subscribe to:
Posts (Atom)
