Freigeben über


Property Value Inheritance

Property value inheritance is a feature of the Windows Presentation Foundation (WPF) property system. Property value inheritance enables descendant elements in the logical tree of elements to obtain the value of a particular property via inheriting that value as it was set anywhere in any of its recursive parent elements. Property value inheritance is not the default property system behavior; a property must be established with a particular metadata setting in order to cause that property to participate in property value inheritance.

This topic contains the following sections.

  • Property Value Inheritance is Containment Inheritance
  • Practical Applications of Property Value Inheritance
  • Making a Custom Property Inheritable
  • Related Topics

Property Value Inheritance is Containment Inheritance

"Inheritance" as a term here is not quite the same concept as how inheritance works in general object oriented programming, where subclasses inherit member definitions from their base classes. That meaning of inheritance is also active in Windows Presentation Foundation (WPF): properties defined in various base classes are exposed as attributes for subclassed XAML elements, and exposed as members for programmatic access. Property value inheritance is particularly about how property values can inherit from one object to another on basis of their parent-child relationships within a logical tree of elements. That logical tree of elements is most directly visible when nesting various elements inside other elements when creating applications in XAML markup. Logical trees can also be created programmatically by adding objects to designated collections of other objects, and property value inheritance works the same way in the finished logical tree at run time.

Practical Applications of Property Value Inheritance

The Windows Presentation Foundation (WPF) APIs include several properties that have property inheritance enabled. Typically, the scenario for these is that they involve a property where it is appropriate that the property be set only once per page, but where that property is also a member of one of the base element classes and thus would also exist on most of the child elements. For example, the FlowDirection property controls which direction flowed content should be presented and arranged on the page. Normally you want this to be handled consistently throughout an element's child elements, and if it were for some reason reset in some level of the logical tree it should probably be reset throughout. By making this property inherit, the value need only be set or reset once at the level in the logical tree that encompasses the presentation needs of each page in the application, and even the initial default value will inherit in this way. Yet the inheritance model still allows individual elements to have the value reset for the rare cases where having a mix of flow directions was intended.

Making a Custom Property Inheritable

By changing a custom property's metadata, you can also make your own custom properties inheritable. Note however that designating a property as inheritable does have some performance considerations. In cases where that property does not have an established local value, or a value obtained through styles, templates, or data binding, an inheritable property will provide its assigned property values to all child elements in the logical tree.

To make a property participate in value inheritance, create a custom dependency property, as described in the Custom Dependency Properties topic. Register the property with metadata (FrameworkPropertyMetadata) and specify the "Inherits" option in the options settings within that metadata. Also make sure that the property has an established default value.

See Also

Concepts

Dependency Property Metadata