DependencyPropertyKey.OverrideMetadata(Type, PropertyMetadata) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重写由此依赖属性标识符表示的只读依赖属性的元数据。
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 实例作为键参数传递。