Partilhar via


Herança de Valor de Propriedade

Property value inheritance is a feature of the Windows Presentation Foundation (WPF) property system. Property value inheritance enables child elements in a tree of elements to obtain the value of a particular property from parent elements, inheriting that value as it was set anywhere in the nearest parent element. O elemento pai pode também ter obtido seu valor através de herança de valor de propriedade, portanto, o sistema potencialmente recurses até a raiz da página. 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 initiate property value inheritance on child elements.

Este tópico contém as seguintes seções.

  • Property Value Inheritance Is Containment Inheritance
  • Practical Applications of Property Value Inheritance
  • Making a Custom Property Inheritable
  • Inheriting Property Values Across Tree Boundaries
  • Tópicos relacionados

Property Value Inheritance Is Containment Inheritance

"Inheritance" as a term here is not quite the same concept as inheritance in the context of types and general object-oriented programming, where derived classes inherit member definitions from their base classes. O significado de herança também está ativo no WPF: propriedades definidas em várias classes base são expostas como atributos para derivado de XAML classes quando usados como elementos e expostos como membros para o código. Herança de valor da propriedade é particularmente sobre como os valores de propriedade podem herdar de um elemento para outro na Base de relações pai-filho em uma árvore de elementos. Árvore de elementos é mais diretamente visível quando o aninhamento de elementos dentro de outros elementos conforme você define os aplicativos em XAML marcação. Trees of objects 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 tree at run time.

Practical Applications of Property Value Inheritance

O WPF APIs incluem várias propriedades que possuem a herança de propriedade ativada. 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. Typically, you want the text flow concept to be handled consistently throughout all child elements. If flow direction were for some reason reset in some level of the element tree by user or environment action, it should typically be reset throughout. When the FlowDirection property is made to inherit, the value need only be set or reset once at the level in the element tree that encompasses the presentation needs of each page in the application. Even the initial default value will inherit in this way. The property value inheritance model still enables individual elements to reset the value for the rare cases where having a mix of flow directions is intentional.

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 provides its assigned property values to all child elements in the logical tree.

To make a property participate in value inheritance, create a custom attached property, as described in Como: Register an Attached Property. 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, because that value will now inherit. Embora a propriedade é registrado como anexado, também convém criar uma propriedade "empacotador" para acesso de get/set no tipo de proprietário, exatamente como faria para um "não anexado" propriedade de dependência. After doing so, the inheritable property can either be set by using the direct property wrapper on the owner type or derived types, or it can be set by using the attached property syntax on any DependencyObject.

Attached properties are conceptually similar to global properties; you can check for the value on any DependencyObject and get a valid result. The typical scenario for attached properties is to set property values on child elements, and that scenario is more effective if the property in question is an attached property that is always implicitly present as an attached property on each element (DependencyObject) in the tree.

Observação

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 run-time tree is undefined.Always use RegisterAttached to register properties where you specify Inherits in the metadata.

Inheriting Property Values Across Tree Boundaries

Property inheritance works by traversing a tree of elements. This tree is often parallel to the logical tree. However, whenever you include a WPF core-level object in the markup that defines an element tree, such as a Brush, you have created a discontinuous logical tree. A true logical tree does not conceptually extend through the Brush, because the logical tree is a WPF framework-level concept. You can see this reflected in the results when using the methods of LogicalTreeHelper. However, property value inheritance can bridge this gap in the logical tree and can still pass inherited values through, so long as the inheritable property was registered as an attached property and no deliberate inheritance-blocking boundary (such as a Frame) is encountered.

Consulte também

Conceitos

Metadados de Propriedade de Dependência

Attached Properties Overview

Precedência de valores de propriedade de dependência