框架属性元数据Framework Property Metadata

对于 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 体系结构中被视为 WPF 框架级的对象元素的属性,将报告框架属性元数据选项。Framework property metadata options are reported for the properties of object elements considered to be at the WPF framework level in the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) architecture. 通常,WPF 框架级别的指定要求由 WPFWPF 表示 Api 和可执行文件处理呈现、数据绑定和属性系统优化等功能。In general the WPF framework-level designation entails that features such as rendering, data binding, and property system refinements are handled by the WPFWPF presentation APIs and executables. 这些系统对框架属性元数据进行查询,以便为特定的元素属性确定特定于功能的特征。Framework property metadata is queried by these systems to determine feature-specific characteristics of particular element properties.

PrerequisitesPrerequisites

本主题假定你从 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 类的现有依赖属性的使用者角度了解依赖属性,并且已阅读依赖属性概述This topic assumes that you understand dependency properties from the perspective of a consumer of existing dependency properties on Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) classes, and have read the Dependency Properties Overview. 另外,应当已阅读依赖属性元数据You should also have read Dependency Property Metadata.

框架属性元数据传达的信息What Is Communicated by Framework Property Metadata

框架属性元数据分为以下类别:Framework property metadata can be divided into the following categories:

  • 报表影响元素的布局属性(AffectsArrangeAffectsMeasureAffectsRender)。Reporting layout properties that affect an element (AffectsArrange, AffectsMeasure, AffectsRender). 如果属性影响各个方面,则可以在元数据中设置这些标志,并且还会在类中实现 MeasureOverride / ArrangeOverride 方法,以便向布局系统提供特定的呈现行为和信息。You might set these flags in metadata if the property affects those respective aspects, and you are also implementing the MeasureOverride / ArrangeOverride methods in your class to supply specific rendering behavior and information to the layout system. 通常,如果在属性元数据中有布局属性为 true,则此类实现会在依赖属性中检查属性是否无效,并且只有无效属性才必须请求新的布局处理过程。Typically, such an implementation would check for property invalidations in dependency properties where any of these layout properties were true in the property metadata, and only those invalidations would necessitate requesting a new layout pass.

  • 报告影响元素的父元素的布局属性(AffectsParentArrangeAffectsParentMeasure)。Reporting layout properties that affect the parent element of an element (AffectsParentArrange, AffectsParentMeasure). 默认情况下,将设置这些标志 FixedPage.LeftParagraph.KeepWithNextSome examples where these flags are set by default are FixedPage.Left and Paragraph.KeepWithNext.

  • InheritsInherits. 默认情况下,依赖属性不会继承值。By default, dependency properties do not inherit values. OverridesInheritanceBehavior 允许将继承路径传递到可视化树,这对于某些控件组合方案来说是必需的。OverridesInheritanceBehavior allows the pathway of inheritance to also travel into a visual tree, which is necessary for some control compositing scenarios.

    备注

    属性值上下文中的术语“继承”指特定于依赖属性的情况;它意味着由于存在 WPFWPF 属性系统的 WPF 框架级功能,子元素可以从父元素继承实际的依赖属性值。The term "inherits" in the context of property values means something specific for dependency properties; it means that child elements can inherit the actual dependency property value from parent elements because of a WPF framework-level capability of the WPFWPF property system. 它与通过派生类型的托管代码类型和成员继承没有直接关系。It has nothing to do directly with managed code type and members inheritance through derived types. 有关详细信息,请参阅属性值继承For details, see Property Value Inheritance.

  • 报告数据绑定特性(IsNotDataBindableBindsTwoWayByDefault)。Reporting data binding characteristics (IsNotDataBindable, BindsTwoWayByDefault). 默认情况下,框架中的依赖属性支持具有单向绑定行为的数据绑定。By default, dependency properties in the framework support data binding, with a one-way binding behavior. 如果没有此类应用程序的方案,则可以禁用数据绑定(因为它们是灵活且可扩展的,所以在默认 WPFWPF Api 中没有此类属性的很多示例)。You might disable data binding if there were no scenario for it whatsoever (because they are intended to be flexible and extensible, there aren't many examples of such properties in the default WPFWPF APIs). 你可以将绑定设置为对在其组件块(例如IsSubmenuOpen)中的控件行为相互关联的属性使用双向默认值,或者双向绑定是用户的常见和预期方案(Text 为示例)。You might set binding to have a two-way default for properties that tie together a control's behaviors amongst its component pieces (IsSubmenuOpen is an example) or where two-way binding is the common and expected scenario for users (Text is an example). 更改与数据绑定相关的元数据只会影响默认值;始终可以根据各个绑定更改此默认值。Changing the data binding–related metadata only influences the default; on a per-binding basis that default can always be changed. 若要详细了解常规的绑定模式和绑定,请参阅数据绑定概述For details on the binding modes and binding in general, see Data Binding Overview.

  • 报告支持日志记录的应用程序或服务是否应记录属性(Journal)。Reporting whether properties should be journaled by applications or services that support journaling (Journal). 对于一般的元素,默认情况下不会启用日记记录功能,但可针对特定用户输入控件有选择性地启用。For general elements, journaling is not enabled by default, but it is selectively enabled for certain user input controls. 此属性专门由日记记录的 WPFWPF 实现等日记记录服务读取,并且通常针对用户控件(例如用户在列表中选择的应在各导航步骤中保留的控件)设置此属性。This property is intended to be read by journaling services including the WPFWPF implementation of journaling, and is typically set on user controls such as user selections within lists that should be persisted across navigation steps. 有关日记的信息,请参阅导航概述For information about the journal, see Navigation Overview.

读取 FrameworkPropertyMetadataReading FrameworkPropertyMetadata

以上链接的每个属性都是 FrameworkPropertyMetadata 添加到其直接基类 UIPropertyMetadata的特定属性。Each of the properties linked above are the specific properties that the FrameworkPropertyMetadata adds to its immediate base class UIPropertyMetadata. 默认情况下,这些属性都为 falseEach of these properties will be false by default. 在知道这些属性的值的情况下,属性的元数据请求应尝试将返回的元数据强制转换为 FrameworkPropertyMetadata,然后根据需要检查各个属性的值。A metadata request for a property where knowing the value of these properties is important should attempt to cast the returned metadata to FrameworkPropertyMetadata, and then check the values of the individual properties as needed.

指定元数据Specifying Metadata

当你创建新的元数据实例以将元数据应用于新的依赖属性注册时,你可以选择要使用的元数据类:基本 PropertyMetadata 或某个派生类(如 FrameworkPropertyMetadata)。When you create a new metadata instance for purposes of applying metadata to a new dependency property registration, you have the choice of which metadata class to use: the base PropertyMetadata or some derived class such as FrameworkPropertyMetadata. 通常情况下,应使用 FrameworkPropertyMetadata,特别是在属性与属性系统和 WPFWPF 函数(如布局和数据绑定)的任何交互时。In general, you should use FrameworkPropertyMetadata, particularly if your property has any interaction with property system and WPFWPF functions such as layout and data binding. 对于更复杂的方案,另一个选项是从 FrameworkPropertyMetadata 派生,以创建自己的元数据报告类,并在其成员中携带额外的信息。Another option for more sophisticated scenarios is to derive from FrameworkPropertyMetadata to create your own metadata reporting class with extra information carried in its members. 或者,您可以使用 PropertyMetadataUIPropertyMetadata 来传达对实现功能的支持程度。Or you might use PropertyMetadata or UIPropertyMetadata to communicate the degree of support for features of your implementation.

对于现有属性(AddOwnerOverrideMetadata 调用),应始终使用初始注册使用的元数据类型重写。For existing properties (AddOwner or OverrideMetadata call), you should always override with the metadata type used by the original registration.

如果要创建 FrameworkPropertyMetadata 实例,可通过两种方法使用传达框架属性特征的特定属性的值填充该元数据:If you are creating a FrameworkPropertyMetadata instance, there are two ways to populate that metadata with values for the specific properties that communicate the framework property characteristics:

  1. 使用允许 flags 参数 FrameworkPropertyMetadata 构造函数签名。Use the FrameworkPropertyMetadata constructor signature that allows a flags parameter. 应为此参数填充 FrameworkPropertyMetadataOptions 枚举标志的所有所需的组合值。This parameter should be filled with all desired combined values of the FrameworkPropertyMetadataOptions enumeration flags.

  2. 使用不带 flags 参数的其中一个签名,然后将 FrameworkPropertyMetadata 上的每个报表布尔属性设置为每个所需的特征更改 trueUse one of the signatures without a flags parameter, and then set each reporting Boolean property on FrameworkPropertyMetadata to true for each desired characteristic change. 为此,必须在构造具有此依赖属性的任何元素之前对这些属性进行设置;布尔属性是可读写属性,以允许此项避免 flags 参数的行为,同时仍然可以填充元数据,但元数据必须在属性使用之前进行有效密封。If you do this, you must set these properties before any elements with this dependency property are constructed; the Boolean properties are read-write in order to allow this behavior of avoiding the flags parameter and still populate the metadata, but the metadata must become effectively sealed before property use. 因此,尝试在请求元数据之后设置属性是无效操作。Thus, attempting to set the properties after metadata is requested will be an invalid operation.

框架属性元数据合并行为Framework Property Metadata Merge Behavior

重写框架属性元数据时,会合并或替换不同的元数据特征。When you override framework property metadata, the different metadata characteristics are either merged or replaced.

此行为由 Merge实现,并且可在派生的元数据类上重写。This behavior is implemented by Merge, and can be overridden on derived metadata classes.

请参阅See also