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 

参数

d
DependencyObject

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

baseValue
Object

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

返回值

Object

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

继承
CoerceValueCallback

示例

下面的示例包括此回调的一个实现, 该实现基于其他输入 (如其他属性的值) 强制转换依赖属性存储的值。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值返回到起始值, 这会导致UIUI (未显示) 从有效选项中删除该下拉列表。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 UIUI (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或派生类, FrameworkPropertyMetadata如)。Each 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属性系统或任何其他调用方对DependencyObject实例调用CoerceValuedp, 都将调用依赖属性的, 同时将该属性的标识符指定为。The 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为任何给定的依赖属性指定多个 (对于已经具有的CoerceValueCallback依赖属性, 重写或添加新的元数据)。Generally 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). 只有其中一个回调才能执行操作。与DependencyObject调用方相比, 操作回调将应用于继承中派生程度最高的类。Only 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.

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅