ValidateValueCallback 委托

定义

表示用作验证依赖属性有效值的回调的方法。Represents a method used as a callback that validates the effective value of a dependency property.

public delegate bool ValidateValueCallback(System::Object ^ value);
public delegate bool ValidateValueCallback(object value);
type ValidateValueCallback = delegate of obj -> bool
Public Delegate Function ValidateValueCallback(value As Object) As Boolean 

参数

value
Object

要验证的值。The value to be validated.

返回值

Boolean

如果该值通过了验证,则为 true;如果提交的值无效,则为 falsetrue if the value was validated; false if the submitted value was invalid.

继承
ValidateValueCallback

示例

下面的示例包括此回调的实现, 用于验证自定义依赖项属性的值的范围。The following example includes an implementation of this callback to validate a range of values for a custom dependency property. 在这种情况下, 属性值需要一个枚举, 而验证确保所提供的值的计算结果为该枚举的成员。In this case the property value expects an enumeration, and the validation assures that the provided value evaluates to a member of that enumeration.

private static bool ShirtValidateCallback(object value)
{
    ShirtTypes sh = (ShirtTypes) value;
    return (sh==ShirtTypes.None || sh == ShirtTypes.Bowling || sh == ShirtTypes.Dress || sh == ShirtTypes.Rugby || sh == ShirtTypes.Tee);

}
Private Shared Function ShirtValidateCallback(ByVal value As Object) As Boolean
    Dim sh As ShirtTypes = CType(value, ShirtTypes)
    Return (sh=ShirtTypes.None OrElse sh = ShirtTypes.Bowling OrElse sh = ShirtTypes.Dress OrElse sh = ShirtTypes.Rugby OrElse sh = ShirtTypes.Tee)

End Function

注解

基于此委托的回调由某些签名DependencyProperty.Register和相关方法 ( RegisterAttached如和只读等效项) 使用。Callbacks based on this delegate are used by certain signatures of DependencyProperty.Register and related methods, such as RegisterAttached and read-only equivalents. 如果希望在每次设置属性validateValueCallback值的有效性时, 对属性值进行特定验证, 则应使用采用参数的签名。You should use signatures that take the validateValueCallback parameter if you want specific validation of the property value each time that its effective is set. 然后, 你应该实现此回调以执行实际的建议值验证。You should then implement this callback such that it performs the actual validation of the proposed value. 如果提交给回true叫的值有效false , 则回调应返回, 否则返回。The callback should return true if the value submitted to the callback is valid, and false otherwise. false值将在尝试了无效属性集的属性系统的特定部分产生异常, 因此应用程序应准备好处理这些异常。A false value will produce an exception in the specific portion of the property system where the invalid property set was attempted, so your application should be prepared to handle these exceptions.

验证回调存储在依赖项属性标识符上, 而不是依赖项属性元数据。Validation callbacks are stored on dependency property identifiers, rather than dependency property metadata. 验证回调无权访问在其上设置属性的的DependencyObject特定实例, 并且只能影响属性通常接受的值。Your validation callback does not have access to a specific instance of a DependencyObject on which the property is set, and can only influence what values are accepted for the property in general. 如果需要可基于特定实例更改属性值的回调, 则应结合CoerceValueCallback使用和PropertyChangedCallback回调, 将其应用于属性的属性元数据。If you need a callback that can change property values based on a specific instance, you should use a combination of a CoerceValueCallback and PropertyChangedCallback callbacks as applied to the property metadata of your property. 你还可以将这些回调应用于应影响属性值的相关属性。You might also apply these callbacks to related properties that should influence your property's value. 有关详细信息, 请参阅依赖属性回调和验证For details, see Dependency Property Callbacks and Validation.

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅