日期:2014-05-17  浏览次数:20995 次

C# – CoerceValueCallback合并、替换元数据值

Topic: C# – CoerceValueCallback

标题: C# – CoerceValueCallback合并、替换元数据值

像在the PropertyMetadta.CoerceValueCallback property中说道

As in the references page?the PropertyMetadta.CoerceValueCallback property

?

属性元数据的该回调方法一般不是公有方法,所以, 这个值通常情况下对于只是使用dependency property元数据的条件下不是很重要。使用该属性的一个可能原因是元数据的子类可以应用他们合并逻辑,如果基类和重载类有这个CoerceCallback属性。一般的,CoerceValueCallback的默认合并逻辑是替换。

The callbacks in property metadata are not typically public members on the containing type, so the value of this property is not important for most scenarios that just consume an existing dependency property's metadata. One reason this property is exposed is so that metadata subclasses can perform their desired merge logic if both base metadata and overriding/adding metadata specify a?CoerceValueCallback. However, the default merge logic for aCoerceValueCallback?is to replace the previous one.

在CoerceValueCallback Delegate一文中:

And in the?CoerceValueCallback Delegate

?

CoerceValueCallback调用时间可以是属性系统的任何时间或者是DependencyObjects上显示的调用CoerceValue 方法,调用时,该属性的标识符作为参数dp的值。

The?CoerceValueCallback?for a dependency property is invoked any time that the property system or any other caller calls?CoerceValue?on aDependencyObject?instance, specifying that property's identifier as the?dp.

使用CoerceValueCallback的最大可能原因是当你希望在子类中重载基类并提供一个计算在子类中计算重载的dependency property的值的方法。(比如通过OverrideMetaData(Type, PropertyMetadata)...重载父类中dependency property的值).

The biggest reason that you would use a CoerceValueCallback is that when you try to override a base class and try to provide new method to calculate the value of a dependency property that the derived classes override from the base class (e.g. with the OverrideMetaData(Type, PropertyMetadata)...