相依性屬性值優先順序Dependency Property Value Precedence

本主題說明 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 屬性系統的運作方式如何影響相依性屬性的值,並描述屬性系統套用到屬性有效值的優先順序。This topic explains how the workings of the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) property system can affect the value of a dependency property, and describes the precedence by which aspects of the property system apply to the effective value of a property.

必要條件Prerequisites

本主題假設您已從 WPFWPF 類別的現有相依性屬性消費者角度了解相依性屬性,並已閱讀相依性屬性概觀This topic assumes that you understand dependency properties from the perspective of a consumer of existing dependency properties on WPFWPF classes, and have read Dependency Properties Overview. 為遵循本主題中的範例,您也應該了解 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 並知道如何撰寫 WPFWPF 應用程式。To follow the examples in this topic, you should also understand Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) and know how to write WPFWPF applications.

WPF 屬性系統The WPF Property System

WPFWPF 屬性系統提供一個強大的方法,透過各種因素來決定相依性屬性的值,並啟用即時屬性驗證、晚期繫結,以及將其他屬性的值變更通知相關屬性等功能。The WPFWPF property system offers a powerful way to have the value of dependency properties be determined by a variety of factors, enabling features such as real-time property validation, late binding, and notifying related properties of changes to values for other properties. 用來決定相依性屬性值的實際順序和邏輯相當複雜。The exact order and logic that is used to determine dependency property values is reasonably complex. 不過,知道此順序將有助於避免不必要的屬性設定,也可釐清試圖影響或預測相依性屬性值,為何最後並未產生所預期的值。Knowing this order will help you avoid unnecessary property setting, and might also clear up confusion over exactly why some attempt to influence or anticipate a dependency property value did not end up resulting in the value you expected.

相依性屬性可能在多處「設定」Dependency Properties Might Be "Set" in Multiple Places

下面是同一屬性XAMLXAMLBackground) 具有可能影響該值的三個不同的"設置"操作的示例。The following is example XAMLXAML where the same property (Background) has three different "set" operations that might influence the value.

    <Button Background="Red">
      <Button.Style>
        <Style TargetType="{x:Type Button}">
          <Setter Property="Background" Value="Green"/>
          <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="Blue" />
            </Trigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
Click
    </Button>

在此,您預期會套用哪個色彩 - 紅色、綠色,還是藍色?Here, which color do you expect will apply—red, green, or blue?

除了動畫值和強制型轉之外,區域屬性集會設為最高優先順序。With the exception of animated values and coercion, local property sets are set at the highest precedence. 如果您在本機設定值,可預期該值將會被接受,甚至優先於任何樣式或控制項範本。If you set a value locally you can expect that the value will be honored, even above any styles or control templates. 在此示例中,Background在本地設置為紅色。Here in the example, Background is set to Red locally. 因此,在此作用域中定義的樣式(即使它是隱式樣式,否則將應用於該作用域中該類型的所有元素)不是給予Background屬性其值的最高優先順序。Therefore, the style defined in this scope, even though it is an implicit style that would otherwise apply to all elements of that type in that scope, is not the highest precedence for giving the Background property its value. 如果從該 Button 執行個體移除區域數值 Red,則樣式會具有優先順序,因此按鈕會從樣式取得 Background 值。If you removed the local value of Red from that Button instance, then the style would have precedence and the button would obtain the Background value from the style. 在樣式內,觸發程序具有較高的優先順序,因此如果將滑鼠移至按鈕上方,按鈕會變成藍色,否則就會是綠色的。Within the style, triggers take precedence, so the button will be blue if the mouse is over it, and green otherwise.

相依性屬性設定優先順序清單Dependency Property Setting Precedence List

以下是屬性系統在指派相依性屬性的執行階段值時,所使用的決定順序。The following is the definitive order that the property system uses when assigning the run-time values of dependency properties. 最高優先順序會先列出。Highest precedence is listed first. 此清單更進一步說明了相依性屬性概觀中所述的部分概要。This list expands on some of the generalizations made in the Dependency Properties Overview.

  1. 屬性系統強制型轉Property system coercion. 如需強制型轉的詳細資訊,請參閱本主題稍後的強制型轉、動畫和基底值For details on coercion, see Coercion, Animation, and Base Value later in this topic.

  2. 作用中動畫或具有 Hold 行為的動畫Active animations, or animations with a Hold behavior. 屬性的動畫必須優先於基底 (非動畫) 值,才能有任何實際效果,即使該值是在本機設定也一樣。In order to have any practical effect, an animation of a property must be able to have precedence over the base (unanimated) value, even if that value was set locally. 如需詳細資訊,請參閱本主題稍後的強制型轉、動畫和基底值For details, see Coercion, Animation, and Base Value later in this topic.

  3. 區域數值Local value. 本地值可以通過"wrapper"屬性的便利性進行設置,該屬性也等同于在 中XAMLXAML設置為屬性或屬性元素,或者通過使用特定實例的屬性調用SetValueAPI。A local value might be set through the convenience of the "wrapper" property, which also equates to setting as an attribute or property element in XAMLXAML, or by a call to the SetValue API using a property of a specific instance. 如果使用繫結或資源來設定區域數值,其在優先順序中的作用就如同設定直接值。If you set a local value by using a binding or a resource, these each act in the precedence as if a direct value was set.

  4. TemplatedParent 範本屬性TemplatedParent template properties. TemplatedParent ControlTemplate元素DataTemplate具有An element has a TemplatedParent if it was created as part of a template (a ControlTemplate or DataTemplate). 如需何時套用此屬性的詳細資訊,請參閱本主題稍後的 TemplatedParentFor details on when this applies, see TemplatedParent later in this topic. 在範本內,優先順序如下:Within the template, the following precedence applies:

    1. 來自範本的TemplatedParent觸發器。Triggers from the TemplatedParent template.

    2. 範本中的屬性集(XAMLXAML通常通過屬性)。 TemplatedParentProperty sets (typically through XAMLXAML attributes) in the TemplatedParent template.

  5. 隱含樣式Implicit style. 僅適用於 Style 屬性。Applies only to the Style property. Style 屬性是由具有符合項目類型之索引鍵的任何樣式資源所填入。The Style property is filled by any style resource with a key that matches the type of that element. 該樣式資源必須位於頁面或應用程式中;查閱隱含樣式資源不會進行到主題中。That style resource must exist either in the page or the application; lookup for an implicit style resource does not proceed into the themes.

  6. 樣式觸發程序Style triggers. 來自於頁面或應用程式之樣式內的觸發程序 (這些樣式可為明確或隱含樣式,但不能來自於預設樣式,預設樣式的優先順序較低)。The triggers within styles from page or application (these styles might be either explicit or implicit styles, but not from the default styles, which have lower precedence).

  7. 樣板觸發程序Template triggers. 樣式內之範本或直接套用之範本中的任何觸發程序。Any trigger from a template within a style, or a directly applied template.

  8. 樣式 setterStyle setters. 來自頁面或Setter應用程式中的樣式內的值。Values from a Setter within styles from page or application.

  9. 預設 (主題) 樣式Default (theme) style. 如需何時套用此樣式,以及主題樣式與主題樣式內範本之關聯的詳細資訊,請參閱本主題稍後的預設 (主題) 樣式For details on when this applies, and how theme styles relate to the templates within theme styles, see Default (Theme) Styles later in this topic. 在預設樣式內,優先順序如下:Within a default style, the following order of precedence applies:

    1. 主題樣式中的作用中觸發程序。Active triggers in the theme style.

    2. 主題樣式中的 setter。Setters in the theme style.

  10. 繼承。Inheritance. 有幾個相依性屬性會從父項目繼承值到子項目,因此無須在整個應用程式的每一個項目上特別設定。A few dependency properties inherit their values from parent element to child elements, such that they need not be set specifically on each element throughout an application. 有關詳細資訊,請參閱屬性值繼承For details see Property Value Inheritance.

  11. 來自相依性屬性中繼資料的預設值Default value from dependency property metadata. 任何指定的相依性屬性都可能會有屬性系統註冊為該特定屬性建立的預設值。Any given dependency property may have a default value as established by the property system registration of that particular property. 此外,繼承相依性屬性的衍生類別可選擇根據類型覆寫該中繼資料 (包括預設值)。Also, derived classes that inherit a dependency property have the option to override that metadata (including the default value) on a per-type basis. 如需詳細資訊,請參閱相依性屬性中繼資料See Dependency Property Metadata for more information. 因為會在預設值之前檢查繼承,所以對繼承的屬性而言,父項目的預設值會優先於子項目。Because inheritance is checked before default value, for an inherited property, a parent element default value takes precedence over a child element. 因此,如果未在任何位置設定可繼承的屬性,就會使用在根項目或父項目上指定的預設值,而不是子項目的預設值。Consequently, if an inheritable property is not set anywhere, the default value as specified on the root or parent is used instead of the child element default value.

TemplatedParentTemplatedParent

作為優先順序項目的 TemplatedParent 不會套用到您直接在標準應用程式標記中宣告之項目的任何屬性。TemplatedParent as a precedence item does not apply to any property of an element that you declare directly in standard application markup. TemplatedParent 概念只對視覺化樹狀結構內因套用範本而產生的子項目有效。The TemplatedParent concept exists only for child items within a visual tree that come into existence through the application of the template. 當屬性系統搜索TemplatedParent範本查找值時,它將搜索創建該元素的範本。When the property system searches the TemplatedParent template for a value, it is searching the template that created that element. 範本中TemplatedParent的屬性值通常充當子項目上的本地值,但相對於本地值存在此較小的優先順序,因為範本可能共用。The property values from the TemplatedParent template generally act as if they were set as a local value on the child element, but this lesser precedence versus the local value exists because the templates are potentially shared. 如需詳細資訊,請參閱TemplatedParentFor details, see TemplatedParent.

Style 屬性The Style Property

前面描述的查找順序適用于所有可能的依賴項屬性,但屬性Style除外。The order of lookup described earlier applies to all possible dependency properties except one: the Style property. Style屬性是唯一的,因為它本身無法設置樣式,因此優先順序項 5 到 8 不適用。The Style property is unique in that it cannot itself be styled, so the precedence items 5 through 8 do not apply. 此外,不建議進行動畫或強制Style(動畫Style需要自訂動畫類)。Also, either animating or coercing Style is not recommended (and animating Style would require a custom animation class). 這留下了三種設置Style屬性的方法:This leaves three ways that the Style property might be set:

  • 明確樣式Explicit style. 屬性Style是直接設置的。The Style property is set directly. 在大多數情況下,樣式不會內嵌定義,而是使用明確索引鍵將它當做資源參考。In most scenarios, the style is not defined inline, but instead is referenced as a resource, by explicit key. 在此情況下,Style 屬性本身就會當做區域數值,也就是優先順序項目 3。In this case the Style property itself acts as if it were a local value, precedence item 3.

  • 隱含樣式Implicit style. 屬性Style不是直接設置的。The Style property is not set directly. 但是,資源Style查找序列(頁面、應用程式)中的某個級別存在 ,並使用與要應用於樣式的類型匹配的資源鍵進行鍵控。However, the Style exists at some level in the resource lookup sequence (page, application) and is keyed using a resource key that matches the type the style is to be applied to. 在這種情況下,Style屬性本身按序列中標識為專案 5 的優先順序執行。In this case, the Style property itself acts by a precedence identified in the sequence as item 5. 可以通過對DependencyPropertyHelperStyle屬性使用並在結果中查找ImplicitStyleReference來檢測此情況。This condition can be detected by using DependencyPropertyHelper against the Style property and looking for ImplicitStyleReference in the results.

  • 預設樣式也稱為主題樣式Default style, also known as theme style. 屬性Style不是直接設置的,實際上將讀取為null運行時。The Style property is not set directly, and in fact will read as null up until run time. 在此情況下,樣式會來自屬於 WPFWPF 展示引擎一部分的執行階段主題評估。In this case, the style comes from the run-time theme evaluation that is part of the WPFWPF presentation engine.

對於不在主題中的隱式樣式,類型必須完全符合 -MyButton``Button派生類不會隱式使用 樣式ButtonFor implicit styles not in themes, the type must match exactly - a MyButton Button-derived class will not implicitly use a style for Button.

預設 (主題) 樣式Default (Theme) Styles

WPFWPF 隨附的每個控制項都有預設樣式。Every control that ships with WPFWPF has a default style. 該預設樣式可能會視主題而不同,因此預設樣式有時也稱為主題樣式。That default style potentially varies by theme, which is why this default style is sometimes referred to as a theme style.

控制項的預設樣式中找到的最重要資訊是控制項範本,該範本作為其Template屬性的 setter 存在於主題樣式中。The most important information that is found within a default style for a control is its control template, which exists in the theme style as a setter for its Template property. 如果預設樣式沒有範本,則沒有自訂範本作為自訂樣式一部分的控制項將完全沒有視覺外觀。If there were no template from default styles, a control without a custom template as part of a custom style would have no visual appearance at all. 預設樣式中的範本會為每個控制項的視覺外觀提供基本結構,而且也會定義在範本視覺化樹狀結構中定義的屬性與相對應控制項類別之間的連接。The template from the default style gives the visual appearance of each control a basic structure, and also defines the connections between properties defined in the visual tree of the template and the corresponding control class. 每個控制項會公開一組屬性,這些屬性可影響控制項的視覺外觀,但不會完全取代範本。Each control exposes a set of properties that can influence the visual appearance of the control without completely replacing the template. 例如,請考慮Thumb控制項的預設可視外觀,該外觀是 的ScrollBar元件。For example, consider the default visual appearance of a Thumb control, which is a component of a ScrollBar.

AThumb具有某些可自訂的屬性。A Thumb has certain customizable properties. 的預設範本Thumb創建一個基本結構/視覺化樹,其中有多個嵌套Border元件,以創建斜面外觀。The default template of a Thumb creates a basic structure / visual tree with several nested Border components to create a bevel look. 如果作為範本一部分的屬性打算公開以供Thumb類自訂,則該屬性必須由範本中的範本繫結公開。If a property that is part of the template is intended to be exposed for customization by the Thumb class, then that property must be exposed by a TemplateBinding, within the template. 在 中Thumb,這些邊框的各種屬性共用綁定到 屬性(如 或BackgroundBorderThickness) 的範本。In the case of Thumb, various properties of these borders share a template binding to properties such as Background or BorderThickness. 但特定其他屬性或視覺化排列方式是用硬式編碼加入控制項範本,或繫結至直接來自於主題的值,除了取代整個範本之外,無法以其他方式變更。But certain other properties or visual arrangements are hard-coded into the control template or are bound to values that come directly from the theme, and cannot be changed short of replacing the entire template. 一般而言,如果屬性來自於樣板化父項目且不由範本繫結公開,就無法使用樣式調整,因為沒有簡單的方式可將它設為目標。Generally, if a property comes from a templated parent and is not exposed by a template binding, it cannot be adjusted by styles because there is no easy way to target it. 但該屬性仍會受到所套用範本中的屬性值繼承所影響,或受到預設值所影響。But that property could still be influenced by property value inheritance in the applied template, or by default value.

主題樣式在其定義中使用類型作為索引鍵。The theme styles use a type as the key in their definitions. 但是,當主題應用於給定元素實例時,通過檢查控制項上的屬性DefaultStyleKey來執行此類型的主題查找。However, when themes are applied to a given element instance, themes lookup for this type is performed by checking the DefaultStyleKey property on a control. 這與隱含樣式使用常值 Type 的方式相反。This is in contrast to using the literal Type, as implicit styles do. 即使實現者DefaultStyleKey未更改該屬性,也會繼承到派生類的值(更改屬性的預定方式不是在屬性級別重寫該屬性,而是更改屬性中繼資料中的預設值)。The value of DefaultStyleKey would inherit to derived classes even if the implementer did not change it (the intended way of changing the property is not to override it at the property level, but to instead change its default value in property metadata). 這種間接方式可讓基底類別為沒有樣式 (或者更重要的是,在該樣式內沒有範本,因此完全沒有預設視覺外觀) 的衍生項目定義主題樣式。This indirection enables base classes to define the theme styles for derived elements that do not otherwise have a style (or more importantly, do not have a template within that style and would thus have no default visual appearance at all). 因此,您可以派生MyButtonButton並且仍將獲取Button預設範本。Thus, you can derive MyButton from Button and will still get the Button default template. 如果您是 的MyButton控制項作者,並且需要其他行為,則可以重寫DefaultStyleKey依賴MyButton項屬性中繼資料以返回其他鍵,然後定義相關主題樣式,包括必須隨MyButton``MyButton控制項打包的範本。If you were the control author of MyButton and you wanted a different behavior, you could override the dependency property metadata for DefaultStyleKey on MyButton to return a different key, and then define the relevant theme styles including template for MyButton that you must package with your MyButton control. 如需主題、樣式和控制項撰寫的詳細資訊,請參閱控制項撰寫概觀For more details on themes, styles, and control authoring, see Control Authoring Overview.

動態資源參考和繫結Dynamic Resource References and Binding

動態資源參考和繫結作業會採用其設定位置的優先順序。Dynamic resource references and binding operations respect the precedence of the location at which they are set. 例如,套用到區域數值的動態資源會依優先順序項目 3 作用,主題樣式內屬性 setter 的繫結會依優先順序項目 9 套用,依此類推。For example, a dynamic resource applied to a local value acts per precedence item 3, a binding for a property setter within a theme style applies at precedence item 9, and so on. 由於動態資源參考和繫結必須能夠從應用程式的執行階段狀態取得值,因此決定任何指定屬性之屬性值優先順序的實際程序也會延伸至執行階段。Because dynamic resource references and binding must both be able to obtain values from the run time state of the application, this entails that the actual process of determining the property value precedence for any given property extends into the run time as well.

嚴格來說,動態資源參考不是屬性系統的一部分,但它們有自己的查閱順序,會與上面所列的順序互動。Dynamic resource references are not strictly speaking part of the property system, but they do have a lookup order of their own which interacts with the sequence listed above. 該優先順序在 XAML 資源中有更詳細的說明。That precedence is documented more thoroughly in the XAML Resources. 基本上來說,該優先順序就是:項目優先於頁面根項目、應用程式、主題和系統。The basic summation of that precedence is: element to page root, application, theme, system.

動態資源和繫結具有其設定位置的優先順序,但會受到其值影響。Dynamic resources and bindings have the precedence of where they were set, but the value is deferred. 因此,如果您將動態資源或繫結設定為區域數值,區域數值的任何變更都會取代整個動態資源或繫結。One consequence of this is that if you set a dynamic resource or binding to a local value, any change to the local value replaces the dynamic resource or binding entirely. 即使您調用ClearValue方法以清除本地設置的值,也不會還原動態資源或綁定。Even if you call the ClearValue method to clear the locally set value, the dynamic resource or binding will not be restored. 實際上,如果調用ClearValue具有動態資源或綁定的屬性(沒有文本本地值),則ClearValue調用也會清除它們。In fact, if you call ClearValue on a property that has a dynamic resource or binding in place (with no literal local value), they are cleared by the ClearValue call too.

SetCurrentValueSetCurrentValue

該方法SetCurrentValue是設置屬性的另一種方法,但它的優先順序順序不一。The SetCurrentValue method is another way to set a property, but it is not in the order of precedence. 相反,SetCurrentValue使您能夠更改屬性的值,而無需覆蓋前一個值的源。Instead, SetCurrentValue enables you to change the value of a property without overwriting the source of a previous value. 您可以使用SetCurrentValue任何時間設置值,而不給予該值本地值的優先順序。You can use SetCurrentValue any time that you want to set a value without giving that value the precedence of a local value. 例如,如果屬性由觸發器設置,然後通過SetCurrentValue分配另一個值,則屬性系統仍尊重觸發器,如果觸發器的操作發生,該屬性將更改。For example, if a property is set by a trigger and then assigned another value via SetCurrentValue, the property system still respects the trigger and the property will change if the trigger’s action occurs. SetCurrentValue使您能夠更改屬性的值,而不為屬性提供具有更高優先順序的源。SetCurrentValue enables you to change the property’s value without giving it a source with a higher precedence. 同樣,可以使用SetCurrentValue更改屬性的值而不覆蓋綁定。Likewise, you can use SetCurrentValue to change the value of a property without overwriting a binding.

強制型轉、動畫和基底值Coercion, Animations, and Base Value

強制和動畫都作用於此 SDK 中稱為"基值"的值。Coercion and animation both act on a value that is termed as the "base value" throughout this SDK. 因此,基底值是透過在項目中往上評估,直到達到第 2 個項目來決定的值。The base value is thus whatever value is determined through evaluating upwards in the items until item 2 is reached.

對動畫而言,如果動畫未同時指定特定行為的 "From" 和 "To",或是動畫刻意在完成後還原成基底值,則基底值可能會對動畫值造成影響。For an animation, the base value can have an effect on the animated value, if that animation does not specify both "From" and "To" for certain behaviors, or if the animation deliberately reverts to the base value when completed. 若要了解實際的情形,請執行 From、To 和 By 動畫目標值範例To see this in practice, run the From, To, and By Animation Target Values Sample. 請試著設定此範例中矩形高度的區域數值,讓初始區域數值與動畫中的任何 "From" 不同。Try setting the local values of the rectangle height in the example, such that the initial local value differs from any "From" in the animation. 您會發現,動畫會立即使用 "From" 值啟動,並在啟動後取代基底值。You will note that the animations start right away using the "From" values and replace the base value once started. 動畫可能指定在動畫完成後通過指定 StopFillBehavior返回到動畫之前找到的值。The animation might specify to return to the value found before animation once it is completed by specifying the Stop FillBehavior. 在此之後,就會使用正常優先順序來決定基底值。Afterwards, normal precedence is used for the base value determination.

您可將多個動畫套用到單一屬性,每個動畫可能已透過值優先順序的不同點定義。Multiple animations might be applied to a single property, with each of these animations possibly having been defined from different points in the value precedence. 不過,這些動畫可能會將值結合起來,而不是只從較高的優先順序套用動畫。However, these animations will potentially composite their values, rather than just applying the animation from the higher precedence. 這完全取決於動畫的定義方式,以及要顯示為動畫之值的類型。This depends on exactly how the animations are defined, and the type of the value that is being animated. 如需將屬性顯示為動畫的詳細資訊,請參閱動畫概觀For more information about animating properties, see Animation Overview.

強制型轉的套用層級最高。Coercion applies at the highest level of all. 即使是已在執行中的應用程式也受值強制型轉的限制。Even an already running animation is subject to value coercion. WPFWPF 中的某些現有相依性屬性有內建強制型轉。Certain existing dependency properties in WPFWPF have built-in coercion. 對於自訂依賴項屬性,通過在創建屬性時編寫 和CoerceValueCallback傳遞回檔作為中繼資料的一部分來定義自訂依賴項屬性的強制行為。For a custom dependency property, you define the coercion behavior for a custom dependency property by writing a CoerceValueCallback and passing the callback as part of metadata when you create the property. 您也可以覆寫現有屬性的強制型轉行為,方式是在衍生類別中覆寫該屬性上的中繼資料。You can also override coercion behavior of existing properties by overriding the metadata on that property in a derived class. 強制型轉與基底值互動的方式是,強制型轉上的條件約束會以當時存在的條件約束套用,但仍保留基底值。Coercion interacts with the base value in such a way that the constraints on coercion are applied as those constraints exist at the time, but the base value is still retained. 因此,如果強制型轉中的條件約束稍後放寬,強制型轉就會傳回最接近該基底值的值,而且強制型轉對屬性的影響有可能會在所有條件約束都放寬之後停止。Therefore, if constraints in coercion are later lifted, the coercion will return the closest value possible to that base value, and potentially the coercion influence on a property will cease as soon as all constraints are lifted. 如需強制型轉行為的詳細資訊,請參閱相依性屬性回呼和驗證For more information about coercion behavior, see Dependency Property Callbacks and Validation.

觸發程序行為Trigger Behaviors

控制項通常會將觸發程序行為定義為其在主題中之預設樣式的一部分。Controls often define trigger behaviors as part of their default style in themes. 在控制項上設定區域屬性,可能會使觸發程序無法從視覺或行為上回應使用者驅動的事件。Setting local properties on controls might prevent the triggers from being able to respond to user-driven events either visually or behaviorally. 屬性觸發器的最常見用途是控制項或狀態屬性(如IsSelectedThe most common use of a property trigger is for control or state properties such as IsSelected. 例如,預設情況下,當禁用Button(為IsEnabledisfalse的觸發器)Foreground時,主題樣式中的值是導致控制項顯示為"灰顯"的原因。For example, by default when a Button is disabled (trigger for IsEnabled is false) then the Foreground value in the theme style is what causes the control to appear "grayed out". 但是,如果您設置了本地Foreground值,則即使在此屬性觸發的方案中,該正常的灰出顏色也會優先于本地屬性集來否決。But if you have set a local Foreground value, that normal gray-out color will be overruled in precedence by your local property set, even in this property-triggered scenario. 當您為具有主題層級觸發程序行為的屬性設定值時,請特別小心,並確保不會不當干擾該控制項的預期使用者體驗。Be cautious of setting values for properties that have theme-level trigger behaviors and make sure you are not unduly interfering with the intended user experience for that control.

ClearValue 和值優先順序ClearValue and Value Precedence

該方法ClearValue提供了一個權宜之計,用於清除在元素上設置的依賴項屬性中的任何本地應用值。The ClearValue method provides an expedient means to clear any locally applied value from a dependency property that is set on an element. 但是,調用ClearValue並不保證在屬性註冊期間在中繼資料中建立的預設值是新的有效值。However, calling ClearValue is not a guarantee that the default as established in metadata during property registration is the new effective value. 值優先順序中的所有其他參與者仍在作用中。All of the other participants in value precedence are still active. 只有在本機設定的值會從優先順序移除。Only the locally set value has been removed from the precedence sequence. 例如,如果調用ClearValue由主題樣式設置該屬性的屬性,則主題值將應用為新值,而不是基於中繼資料的預設值。For example, if you call ClearValue on a property where that property is also set by a theme style, then the theme value is applied as the new value rather than the metadata-based default. 如果要將所有屬性值參與者從流程中拉出,並將該值設置為已註冊的中繼資料預設值,則可以通過查詢依賴項屬性中繼資料最終獲取該預設值,然後可以使用預設值在本地設置具有 調用 的屬性SetValueIf you want to take all property value participants out of the process and set the value to the registered metadata default, you can obtain that default value definitively by querying the dependency property metadata, and then you can use the default value to locally set the property with a call to SetValue.

另請參閱See also