CoerceValueCallback 委托


为只要重新计算依赖项属性值或专门请求强制转换时就调用的方法提供一个模板。Provides a template for a method that is called whenever a dependency property value is being re-evaluated, or coercion is specifically requested.

public delegate System::Object ^ CoerceValueCallback(DependencyObject ^ d, System::Object ^ baseValue);
public delegate object CoerceValueCallback(DependencyObject d, object baseValue);
type CoerceValueCallback = delegate of DependencyObject * obj -> obj
Public Delegate Function CoerceValueCallback(d As DependencyObject, baseValue As Object) As Object 



该属性所在的对象。The object that the property exists on. 在调用该回调时,属性系统将会传递该值。When the callback is invoked, the property system will pass this value.


该属性在尝试执行任何强制转换之前的新值。The new value of the property, prior to any coercion attempt.



强制转换后的值(采用适当的类型)。The coerced value (with appropriate type).


下面的示例包括此回调的一个实现,该实现基于其他输入(如其他属性的值)强制转换依赖属性存储的值。The following example includes an implementation of this callback to coerce the stored value of a dependency property based on other inputs, such as another property's value. 在这种情况下,回调会检查属性是否 ShirtType 对应于具有按钮的衬衫类型; 如果为,则它将为设置起始默认颜色; 如果 ButtonColor 衬衫类型没有按钮,则会将该值强制转换 ButtonColor 为起始值,这会导致 UI (不会显示) 从有效选项中删除该下拉列表。In this case, the callback checks to see whether the ShirtType property corresponds to a type of shirt that has buttons; if so it establishes a starting default color for the ButtonColor, if the shirt type has no buttons, it coerces the ButtonColor value back to a starting value, which causes the UI (not shown) to remove that dropdown from the effective choices.

private static object CoerceButtonColor(DependencyObject d, object value)
    ShirtTypes newShirtType = (d as Shirt).ShirtType;
    if (newShirtType == ShirtTypes.Dress || newShirtType == ShirtTypes.Bowling)
        return ButtonColors.Black;				
    return ButtonColors.None;
Private Shared Function CoerceButtonColor(ByVal d As DependencyObject, ByVal value As Object) As Object
    Dim newShirtType As ShirtTypes = (TryCast(d, Shirt)).ShirtType
    If newShirtType = ShirtTypes.Dress OrElse newShirtType = ShirtTypes.Bowling Then
        Return ButtonColors.Black
    End If
    Return ButtonColors.None
End Function


CoerceValueCallback可以通过多种不同的方法将基于的回调分配给依赖属性。Callbacks based on CoerceValueCallback can be assigned to a dependency property through several different techniques. 每种方法都要求您首先 (PropertyMetadata 或派生类(如) )创建新的属性元数据对象 FrameworkPropertyMetadataEach of these techniques requires that you first create a new property metadata object (PropertyMetadata, or a derived class such as FrameworkPropertyMetadata). 使用采用参数的构造函数签名创建元数据对象 coerceValueCallback ,并将该参数分配给回调处理程序。Create the metadata object using a constructor signature that takes the coerceValueCallback parameter, and assign that parameter to your callback handler. 或按任何签名构造元数据,并 CoerceValueCallback 在将元数据投入使用之前设置属性。Or construct the metadata by any signature and set the CoerceValueCallback property prior to putting the metadata in use.

具有此元数据时,可以:When you have this metadata, you can:

此回调的实现应检查中的值 baseValue ,并根据值或类型确定这是否是需要进一步强制转换的值。Implementations of this callback should check the value in baseValue and determine based on either the value or the type whether this is a value that needs to be further coerced.

每当 CoerceValueCallback 属性系统或任何其他调用方对实例调用时,都将调用依赖属性的 CoerceValue DependencyObject ,同时将该属性的标识符指定为 dpThe CoerceValueCallback for a dependency property is invoked any time that the property system or any other caller calls CoerceValue on a DependencyObject instance, specifying that property's identifier as the dp.

对属性值的更改可能来自属性系统中任何可能的参与者。Changes to the property value may have come from any possible participant in the property system. 这包括样式、泛型失效、触发器、属性值继承和本地值设置。This includes styles, generic invalidation, triggers, property value inheritance, and local value setting.

通常,应避免 CoerceValueCallback 为任何给定的依赖属性指定多个 (使用已有) 的依赖属性的新元数据重写或添加 CoerceValueCallbackGenerally you should avoid specifying more than one CoerceValueCallback for any given dependency property (overriding or adding with new metadata for a dependency property that already had a CoerceValueCallback). 只有其中一个回调才能执行操作。与调用方相比,操作回调将应用于继承中派生程度最高的类 DependencyObjectOnly one of the callbacks will be able to act. The acting callback will be the one that was applied to the most derived class in the inheritance as compared to the DependencyObject caller. 当元数据被重写时,将替换分配给依赖项属性的元数据的其他回调,因为它已存在于所有者层次结构中。Other callbacks as assigned to metadata for the dependency property as it existed higher in the owner hierarchy are replaced when the metadata is overridden.



获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.