DependencyProperty.OverrideMetadata 方法

定义

当此依赖属性位于指定类型的实例上时为其提供替换元数据(而不是在最初注册依赖属性时提供的元数据)。Supplies alternate metadata for this dependency property when it is present on instances of a specified type, versus the metadata that was provided in the initial dependency property registration.

重载

OverrideMetadata(Type, PropertyMetadata)

当此依赖属性位于指定类型的实例上时为其指定替换元数据,以在该依赖属性继承自基类型时重写该属性已存在的元数据。Specifies alternate metadata for this dependency property when it is present on instances of a specified type, overriding the metadata that existed for the dependency property as it was inherited from base types.

OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey)

当只读依赖属性位于指定类型的实例上时为其提供替换元数据,以便重写在最初注册依赖属性时提供的元数据。Supplies alternate metadata for a read-only dependency property when it is present on instances of a specified type, overriding the metadata that was provided in the initial dependency property registration. 你必须为只读依赖属性传递 DependencyPropertyKey,以免引发异常。You must pass the DependencyPropertyKey for the read-only dependency property to avoid raising an exception.

OverrideMetadata(Type, PropertyMetadata)

当此依赖属性位于指定类型的实例上时为其指定替换元数据,以在该依赖属性继承自基类型时重写该属性已存在的元数据。Specifies alternate metadata for this dependency property when it is present on instances of a specified type, overriding the metadata that existed for the dependency property as it was inherited from base types.

public:
 void OverrideMetadata(Type ^ forType, System::Windows::PropertyMetadata ^ typeMetadata);
public void OverrideMetadata (Type forType, System.Windows.PropertyMetadata typeMetadata);
member this.OverrideMetadata : Type * System.Windows.PropertyMetadata -> unit
Public Sub OverrideMetadata (forType As Type, typeMetadata As PropertyMetadata)

参数

forType
Type

一种类型,在该类型上继承该依赖属性并将应用所提供的替换元数据。The type where this dependency property is inherited and where the provided alternate metadata will be applied.

typeMetadata
PropertyMetadata

一种元数据,它将应用于重写类型上的依赖属性。The metadata to apply to the dependency property on the overriding type.

异常

尝试重写只读依赖属性的元数据(不能使用此签名完成该操作)。An attempt was made to override metadata on a read-only dependency property (that operation cannot be done using this signature).

已为依赖属性建立元数据,因为它存在于所提供的类型上。Metadata was already established for the dependency property as it exists on the provided type.

注解

应在属性系统使用依赖属性之前重写依赖属性元数据。Dependency property metadata should be overridden before the property system uses the dependency property. 这相当于使用注册依赖属性的类创建特定实例的时间。This equates to the time that specific instances are created using the class that registers the dependency property. 只应在将自身作为此方法的 forType 参数的类型的静态构造函数中执行,或通过类似的实例化,对 OverrideMetadata 调用。Calls to OverrideMetadata should only be performed within the static constructors of the type that provides itself as the forType parameter of this method, or through similar instantiation. 如果尝试在所有者类型的实例之后更改元数据,则不会引发异常,但会导致在属性系统中出现不一致的行为。Attempting to change metadata after instances of the owner type exist will not raise exceptions, but will result in inconsistent behaviors in the property system.

使用此方法建立特定派生类重写的元数据后,后续尝试重写此同一派生类上的元数据会引发异常。After metadata for a particular derived class override is established with this method, subsequent attempts to override metadata on this same derived class will raise an exception.

提供的元数据与基所有者上存在的依赖属性的属性元数据合并在一起。The supplied metadata is merged with the property metadata for the dependency property as it exists on the base owner. 原始基本元数据中指定的所有特征都将保留;只有那些在新的元数据中特别更改的特性才会重写基本元数据的特性。Any characteristics that were specified in the original base metadata will persist; only those characteristics that were specifically changed in the new metadata will override the characteristics of the base metadata. 如果在新的元数据中指定了某些特征(如 DefaultValue),则会将其替换。Some characteristics such as DefaultValue are replaced if specified in the new metadata. 其他一些(如 PropertyChangedCallback)会合并在一起。Others, such as PropertyChangedCallback, are combined. 最终,合并行为取决于用于重写的属性元数据类型,因此此处所述的行为适用于 WPFWPF 依赖属性使用的现有属性元数据类。Ultimately, the merge behavior depends on the property metadata type being used for the override, so the behavior described here is for the existing property metadata classes used by WPFWPF dependency properties. 有关详细信息,请参阅依赖项属性元数据和框架属性元数据For details, see Dependency Property Metadata and Framework Property Metadata.

OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey)

当只读依赖属性位于指定类型的实例上时为其提供替换元数据,以便重写在最初注册依赖属性时提供的元数据。Supplies alternate metadata for a read-only dependency property when it is present on instances of a specified type, overriding the metadata that was provided in the initial dependency property registration. 你必须为只读依赖属性传递 DependencyPropertyKey,以免引发异常。You must pass the DependencyPropertyKey for the read-only dependency property to avoid raising an exception.

public:
 void OverrideMetadata(Type ^ forType, System::Windows::PropertyMetadata ^ typeMetadata, System::Windows::DependencyPropertyKey ^ key);
public void OverrideMetadata (Type forType, System.Windows.PropertyMetadata typeMetadata, System.Windows.DependencyPropertyKey key);
member this.OverrideMetadata : Type * System.Windows.PropertyMetadata * System.Windows.DependencyPropertyKey -> unit
Public Sub OverrideMetadata (forType As Type, typeMetadata As PropertyMetadata, key As DependencyPropertyKey)

参数

forType
Type

一种类型,在该类型上继承该依赖属性并将应用所提供的替换元数据。The type where this dependency property is inherited and where the provided alternate metadata will be applied.

typeMetadata
PropertyMetadata

一种元数据,它将应用于重写类型上的依赖属性。The metadata to apply to the dependency property on the overriding type.

key
DependencyPropertyKey

只读依赖属性的访问键。The access key for a read-only dependency property.

注解

此签名为只读依赖项属性标识符(DependencyPropertyKey)方法提供基础实现。This signature provides underlying implementation for a read-only dependency property identifier (DependencyPropertyKey) method. 如果重写读写依赖项属性的元数据,请使用 OverrideMetadata(Type, PropertyMetadata)If overriding metadata for a read-write dependency property, use OverrideMetadata(Type, PropertyMetadata).

应在属性系统使用依赖属性之前重写依赖属性元数据。Dependency property metadata should be overridden before the property system uses the dependency property. 这相当于为注册依赖属性的类创建特定对象的时间。This equates to the time that specific objects are created for the class that registers the dependency property. 只应在将自身作为此方法的 forType 参数的类型的静态构造函数中执行,或通过类似的实例化,对 OverrideMetadata 调用。Calls to OverrideMetadata should only be performed within the static constructors of the type that provides itself as the forType parameter of this method, or through similar instantiation. 如果尝试在所有者类型的实例之后更改元数据,则不会引发异常,但会导致在属性系统中出现不一致的行为。Attempting to change metadata after instances of the owner type exist will not raise exceptions, but will result in inconsistent behaviors in the property system.

使用此方法建立特定派生类重写的元数据后,后续尝试重写此同一派生类上的元数据会引发异常。After metadata for a particular derived class override is established with this method, subsequent attempts to override metadata on this same derived class will raise an exception.

提供的元数据与基所有者上存在的依赖属性的属性元数据合并在一起。The supplied metadata is merged with the property metadata for the dependency property as it exists on the base owner. 原始基本元数据中指定的所有特征都将保留;只有那些在新的元数据中特别更改的特性才会重写基本元数据的特性。Any characteristics that were specified in the original base metadata will persist; only those characteristics that were specifically changed in the new metadata will override the characteristics of the base metadata. 如果在新的元数据中指定了某些特征(如 DefaultValue),则会将其替换。Some characteristics such as DefaultValue are replaced if specified in the new metadata. 其他一些(如 PropertyChangedCallback)会合并在一起。Others, such as PropertyChangedCallback, are combined. 合并行为取决于用于重写的属性元数据类型。The merge behavior depends on the property metadata type being used for the override. 有关详细信息,请参阅依赖项属性元数据和框架属性元数据For details, see Dependency Property Metadata and Framework Property Metadata.

适用于