語言

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 。

適用於

另請參閱