依赖项属性值优先级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. 若要采用本主题中的示例,还应当了解可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 并知道如何编写 WPFWPF 应用程序。To follow the examples in this topic, you should also understand 可扩展应用程序标记语言 (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. 样式资源库。Style 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. 主题样式中的资源库。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 属性,实际上会在运行时一直读取为 nullThe 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. 这与使用文本类型的隐式样式正好相反。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 的控件作者并且需要不同的行为,则可以重写 MyButton 上的 DefaultStyleKey 的依赖项属性元数据以返回不同的键,然后定义相关主题样式,包括你必须与 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 进行应用,主题样式中的属性资源库的绑定作为优先级项 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, and By Animation Target Values Sample(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. 动画完成后,可能会指定返回到在动画完成之前找到的值,方法是指定停止 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. 例如,默认情况下,当禁用 ButtonIsEnabled 的触发器 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. 如果要将所有属性值参与者移出进程,并将值设置为已注册的元数据默认值,可以通过查询依赖属性元数据来明确地获取该默认值,然后可以使用默认值在本地设置属性,同时调用 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