屬性值繼承Property Value Inheritance

屬性值繼承是 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 屬性系統的功能。Property value inheritance is a feature of the Windows Presentation Foundation (WPF)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. 父元素可能也會透過屬性值繼承來取得它的值,因此,系統有可能會不停地遞迴到頁面根元素。The parent element might also have obtained its value through property value inheritance, so the system potentially recurses all the way to the page root. 屬性值繼承不是預設的屬性系統行為;屬性必須使用特殊的中繼資料值來建立,才能讓該屬性起始子元素上的屬性值繼承。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.

屬性值繼承是內含項目繼承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. 該繼承的意義也適用於 WPFWPF︰定義於各種基底類別中的屬性 (Property) 均會在衍生的 XAMLXAML 類別用來做為元素時,公開為其屬性 (Attribute),並公開為程式碼的成員。That meaning of inheritance is also active in WPFWPF: properties defined in various base classes are exposed as attributes for derived XAMLXAML classes when used as elements, and exposed as members for code. 屬性值繼承特別是關於屬性值如何根據元素樹狀結構內的父/子關聯性,從某一個元素繼承至另一個元素。Property value inheritance is particularly about how property values can inherit from one element to another on the basis of the parent-child relationships within a tree of elements. 當您在 XAMLXAML 標記中定義應用程式,於其他元素內巢串元素時,幾乎可以直接看見該元素樹狀結構。That tree of elements is most directly visible when nesting elements inside other elements as you define applications in XAMLXAML markup. 物件的樹狀結構也可以透過程式設計方式,將物件新增至其他物件的指定集合來建立,而屬性值繼承會在執行階段,於完成的樹狀結構中以相同方式來運作。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

WPFWPF Api 包括數個已啟用屬性繼承的屬性。The WPFWPF 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. 比方說,FlowDirection屬性會控制哪一個方向流動內容應該呈現,並在頁面上排列。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. FlowDirection; 屬性進行繼承時,值需要只設定或重設一次在包含簡報所需的應用程式中的每一頁的項目樹狀結構的層級。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 Register an Attached Property. 註冊屬性與中繼資料 (FrameworkPropertyMetadata),並在該中繼資料內的 [選項] 設定中指定"Inherits"選項。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. 儘管您已將屬性註冊為附加,但您可能也想要針對擁有者類型上的 get/set 存取建立屬性「包裝函式」,就像您針對「非附加的」相依性屬性所做的一樣。Although you registered the property as attached, you might also want to create a property "wrapper" for get/set access on the owner type, just as you would for an "nonattached" dependency property. 完成後,可繼承的屬性可以設定使用的直接屬性包裝函式上的擁有者型別或衍生型別,或是可以設定任何使用附加的屬性語法DependencyObjectAfter 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.

附加的屬性是概念上類似於全域屬性;您可以檢查任何值DependencyObject並取得有效的結果。Attached properties are conceptually similar to global properties; you can check for the value on any DependencyObject and get a valid result. 附加屬性的典型範例是在子項目上設定屬性值,這種情況下是所討論的屬性是附加的屬性,一律會以隱含方式出現在每個項目上的附加屬性的更有效率 (DependencyObject) 在樹狀目錄中。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.

注意

雖然屬性值繼承似乎適用於非附加的相依性屬性,但在執行階段的樹狀結構中,透過特定元素界限的非附加屬性繼承行為是未定義的。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. 一律使用RegisterAttached登錄,您指定的屬性Inherits中繼資料中。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. 不過,每當您納入 WPF 核心層級物件的標記來定義元素樹狀結構,例如Brush,您已建立的不連續的邏輯樹狀結構。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. 則為 true 的邏輯樹狀結構不會在概念上延伸透過Brush,因為邏輯樹狀結構是 WPF 架構層級概念。A true logical tree does not conceptually extend through the Brush, because the logical tree is a WPF framework-level concept. 您可以看到這反映在結果中的方法時LogicalTreeHelperYou can see this reflected in the results when using the methods of LogicalTreeHelper. 不過,屬性值繼承可以填補此鴻溝邏輯樹狀結構,而且只要可繼承的屬性註冊為附加的屬性和任何特意封鎖繼承的界限,則仍可傳遞繼承的值 (例如Frame) 為止。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.

另請參閱See also