I'm a little bit unclear with all this magic. As I understood dependency properties get inherited from the DependencyObject, so values are stored:
or taken from the link to a parent element if value is not specified.
protected object GetValue(string propertyName)
{
if (LocalValues.ContainsKey(propertyName))
{
return LocalValues[propertyName];
}
return Parent.GetValue(propertyName);
}
Am I correct in this?
I also don't understand where are values for attached properties stored?
Control.FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(Control), new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
FrameworkPropertyMetadataOptions.Inherits));
Does AddOwner method call on Attached property assigns value to the instance field? When does this happen and where does the value go?
Thanks!
The property system in WPF is pretty complex. MSDN really has a lot of information, but it is often hard to find. While there are many ways a DependencyProperty
can be set, I'm not sure that you need to care where the values are stored.
For local values, you can assume that it is stored on the DependencyObject
(again you shouldn't care where it is stored), with the caveat that they are not stored based on strings. It truly is associated with an instance of DependencyProperty
. This is why you would want to add an owner to the property. If somebody sets TextElement.FontSize
on your control, it is just like setting your local FontSize
property.
As far as inheriting values for a property from a parent, this only happens with attached properties. From the MSDN entry for FrameworkPropertyMetadataOptions
:
Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. Always use RegisterAttached to register properties where you specify Inherits in the metadata.