依赖项属性值优先级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) 具有三个不同"set"可能会影响值的操作。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. 样式资源库。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. 可以通过使用检测到该条件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.

最重要的信息的控件为其控件模板,主题样式中存在的资源作为默认样式中找到其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.

一个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,这些边界的各种属性共享模板绑定到属性,如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. 这与使用文本类型的隐式样式正好相反。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 进行应用,主题样式中的属性资源库的绑定作为优先级项 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, 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. 可以指定要返回到是指定 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