DependencyPropertyKey.OverrideMetadata(Type, PropertyMetadata) 方法

定义

重写由此依赖项对象标识符表示的只读依赖项对象的元数据。Overrides the metadata of a read-only dependency property that is represented by this dependency property identifier.

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 on which this dependency property exists and metadata should be overridden.

typeMetadata
PropertyMetadata

为此类型提供的元数据。Metadata supplied for this type.

异常

尝试在读写依赖项对象上进行元数据重写(无法使用此签名完成该操作)。Attempted metadata override on a read-write dependency property (cannot be done using this signature).

当属性存在于提供的类型上时,已为该属性建立元数据。Metadata was already established for the property as it exists on the provided type.

示例

下面的示例将覆盖类继承的现有只读依赖项属性的元数据。The following example overrides metadata for an existing read-only dependency property that a class inherits. 在这种情况下, 方案目标是添加基属性元数据不具有的强制值回调。In this case, the scenario goal was to add a coerce value callback that the base property metadata did not have. 你还可以重写元数据, 因为重写元数据通常是适当的 (更改默认值、添加FrameworkPropertyMetadataOptions值等)。You could also override metadata for any of the other reasons that overriding metadata is typically appropriate (changing default value, adding FrameworkPropertyMetadataOptions values, etc.)

static Fishbowl() {
  Aquarium.AquariumSizeKey.OverrideMetadata(
    typeof(Aquarium),
    new PropertyMetadata(
      double.NaN,
      null,
      new CoerceValueCallback(CoerceFishbowlAquariumSize)
    )
  );
}

static object CoerceFishbowlAquariumSize(DependencyObject d,Object baseValue)
{
    //Aquarium is 2D, a Fishbowl is a round Aquarium, so the Size we return is the ellipse of that height/width rather than the rectangle
    Fishbowl fb = (Fishbowl)d;
    //other constraints assure that H,W are positive
    return Convert.ToInt32(Math.PI * (fb.Width / 2) * (fb.Height / 2));
}
Shared Sub New()
    Aquarium.AquariumSizeKey.OverrideMetadata(GetType(Aquarium), New PropertyMetadata(Double.NaN, Nothing, New CoerceValueCallback(AddressOf CoerceFishbowlAquariumSize)))
End Sub

Private Shared Function CoerceFishbowlAquariumSize(ByVal d As DependencyObject, ByVal baseValue As Object) As Object
    'Aquarium is 2D, a Fishbowl is a round Aquarium, so the Size we return is the ellipse of that height/width rather than the rectangle
    Dim fb As Fishbowl = CType(d, Fishbowl)
    'other constraints assure that H,W are positive
    Return Convert.ToInt32(Math.PI * (fb.Width / 2) * (fb.Height / 2))
End Function

注解

重写只读依赖属性的元数据的方式类似于重写读写依赖项属性的元数据, 并且限制在键级别进行访问, 因为在元数据中指定的行为可以更改集行为 (例如, 默认值为。Overriding metadata on a read-only dependency property is done for similar reasons as overriding metadata on a read-write dependency property, and is restricted to access at the key level because behaviors specified in the metadata can change the set behavior (the default value, for instance).

对于读写依赖属性, 在只读依赖属性上重写元数据时, 只应在属性系统使用该属性之前完成此操作 (这相当于注册属性实例化)。As with read-write dependency properties, overriding metadata on a read-only dependency property should only be done prior to that property being placed in use by the property system (this equates to the time that specific instances of objects that register the property are instantiated). 只应在将自身forType作为此方法的参数的类型的静态构造函数中执行, 或者对该类的等效初始化执行调用。 OverrideMetadataCalls to OverrideMetadata should only be performed within the static constructors of the type that provides itself as the forType parameter of this method, or equivalent initialization for that class.

此方法可以有效地转发OverrideMetadata到方法, DependencyPropertyKey并将实例作为键参数传递。This method effectively forwards to the OverrideMetadata method, passing the DependencyPropertyKey instance as the key parameter.

适用于

另请参阅