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.

適用於

另請參閱