CoerceValueCallback CoerceValueCallback CoerceValueCallback CoerceValueCallback Delegate

定义

为只要重新计算依赖项属性值或专门请求强制转换时就调用的方法提供一个模板。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 DependencyObject DependencyObject DependencyObject

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

baseValue
Object Object Object Object

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

返回值

System.Object

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

继承
CoerceValueCallbackCoerceValueCallbackCoerceValueCallbackCoerceValueCallback

示例

下面的示例包括要强制转换基于其他输入,如另一个属性的值的依赖项属性的存储的值此回调的实现。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属性对应于一种类型的按钮的 shirt; 如果是这样它在建立开始默认颜色ButtonColor,如果 shirt 类型具有没有按钮,它将强制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属性为依赖项调用属性系统或任何其他调用方调用任何时候CoerceValueDependencyObject实例,它指定该属性的标识符作为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对于任何给定的依赖项属性 (重写或添加新的元数据已具有的依赖属性具有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) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

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

适用于

另请参阅