DependencyPropertyKey.OverrideMetadata(Type, PropertyMetadata) 方法

定義

覆寫由相依性屬性識別項表示之唯讀相依性屬性的中繼資料。

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

這個相依性屬性所存在以及中繼資料應該覆寫的型別。

typeMetadata
PropertyMetadata

為這個型別提供的中繼資料。

例外狀況

嘗試覆寫唯讀相依性屬性的中繼資料 (無法使用這個簽章執行)。

已為存在於所提供之型別的屬性建立中繼資料。

範例

下列範例會覆寫類別所繼承之現有唯讀相依性屬性的中繼資料。 在此情況下,案例目標是新增基底屬性中繼資料沒有的強制值回呼。 您也可以針對覆寫中繼資料的任何其他原因覆寫中繼資料,通常 (變更預設值、新增 FrameworkPropertyMetadataOptions 值等等。)

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

備註

覆寫唯讀相依性屬性上的中繼資料是因為覆寫讀寫相依性屬性的類似原因而完成,而且僅限於在索引鍵層級存取,因為中繼資料中指定的行為可以變更設定行為 (預設值,例如) 。

如同讀寫相依性屬性,覆寫唯讀相依性屬性上的中繼資料,應該只在屬性系統使用該屬性之前完成, (這相當於註冊屬性的特定物件實例在具現化) 的時間。 OverrideMetadata對 的呼叫應該只會在型別的靜態建構函式內執行,以提供本身做為 forType 這個方法的參數,或該類別的對等初始化。

這個方法會有效地轉送至 OverrideMetadata 方法,將 DependencyPropertyKey 實例當做 key 參數傳遞。

適用於

另請參閱