相依性屬性值優先順序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

以下是範例XAMLXAML所在的相同屬性 (Background) 有三種不同 「 設定 」 可能會影響值的作業。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. 可能透過方便的 「 包裝函式 」 屬性,這也等同於設定中的屬性或屬性項目設定區域數值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是建立為範本的一部分 (ControlTemplateDataTemplate)。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屬性) 中TemplatedParent範本。Property 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. 使用偵測到此狀況DependencyPropertyHelper對抗Style屬性,並尋找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.

最重要的資訊的控制項是控制項範本,為的 setter 存在佈景主題樣式中的預設樣式內找到其Template屬性。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控制項,也就是元件的ScrollBarFor 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類別,則該屬性必須公開TemplateBinding,範本內。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,這些框線的各種屬性會共用這類屬性的範本繫結BackgroundBorderThicknessIn 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而且您想要不同的行為,您可以覆寫的相依性屬性中繼資料DefaultStyleKeyMyButton傳回不同的金鑰,並接著定義相關的主題樣式,包括範本針對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

在這個 SDKSDK 中,強制型轉和動畫都會在名為「基底值」的值上作用。Coercion and animation both act on a value that is termed as the "base value" throughout this SDKSDK. 因此,基底值是透過在項目中往上評估,直到達到第 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. 動畫可能會傳回由指定 Stop 完成後,動畫之前找到的值指定FillBehaviorThe 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已停用 (觸發IsEnabledfalse) 則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