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

PrerequisitesPrerequisites

本主題假設您已從 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中的屬性或屬性元素,或使用特定實例的屬性呼叫 SetValue API。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. 如果專案是建立為範本的一部分(ControlTemplateDataTemplate),則該專案具有 TemplatedParentAn 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. TemplatedParent 範本中的屬性集(通常是透過 XAMLXAML 屬性)。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. 您可以針對 Style 屬性使用 DependencyPropertyHelper,並在結果中尋找 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.

針對不在主題中的隱含樣式,類型必須完全符合-a MyButton Button衍生類別不會以隱含方式使用 Button的樣式。For 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.

Thumb 有一些可自訂的屬性。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的情況下,這些框線的各種屬性會共用範本系結至屬性(例如 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). 因此,您可以從 Button 衍生 MyButton,而且仍然會取得 Button 預設範本。Thus, you can derive MyButton from Button and will still get the Button default template. 如果您是 MyButton 的控制項作者,而您想要不同的行為,您可以覆寫 MyButtonDefaultStyleKey 的相依性屬性中繼資料,以傳回不同的索引鍵,然後定義相關主題樣式,包括 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. 事實上,如果您在具有動態資源或系結的屬性(不含常值區域數值)上呼叫 ClearValueClearValue 呼叫也會將它們清除。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. 動畫可能會指定在動畫完成後,藉由指定 [停止 FillBehavior] 來返回所找到的值。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 時(IsEnabled 的觸發程式 false),則主題樣式中的 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. 如果您想要讓所有屬性值參與者跳出進程,並將值設定為已註冊的中繼資料預設值,您可以藉由查詢相依性屬性中繼資料來明確取得該預設值,然後您就可以在本機使用預設值使用 SetValue的呼叫來設定屬性。If 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