ValidationRule.Validate 方法

定义

对值执行验证检查。Performs validation checks on a value.

重载

Validate(Object, CultureInfo)

在派生类中重写时,对值执行验证检查。When overridden in a derived class, performs validation checks on a value.

Validate(Object, CultureInfo, BindingExpressionBase)

对值执行验证检查。Performs validation checks on a value.

Validate(Object, CultureInfo, BindingGroup)

对值执行验证检查。Performs validation checks on a value.

Validate(Object, CultureInfo)

在派生类中重写时,对值执行验证检查。When overridden in a derived class, performs validation checks on a value.

public:
 abstract System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo);
public abstract System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo);
abstract member Validate : obj * System.Globalization.CultureInfo -> System.Windows.Controls.ValidationResult
Public MustOverride Function Validate (value As Object, cultureInfo As CultureInfo) As ValidationResult

参数

value
Object

要检查的来自绑定目标的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在此规则中使用的区域性。The culture to use in this rule.

返回

ValidationResult

ValidationResult 对象。A ValidationResult object.

示例

下面的示例演示如何实现验证规则。The following example shows how to implement a validation rule. 在以下示例中,如果输入值包含非数字字符或超出下限,则输入值无效。In the following example, the input value is invalid if it contains non-numeric characters or if it is outside the lower and upper bounds. 如果输入值无效,则将 ErrorContent 属性设置为相应的错误消息,并将 IsValid 属性设置为 falseIf the input value is invalid, the ErrorContent property is set to the appropriate error message and the IsValid property is set to false.

有关完整的示例,请参阅 如何:实现绑定验证For the complete example, see How to: Implement Binding Validation.

public class AgeRangeRule : ValidationRule
{
    public int Min { get; set; }
    public int Max { get; set; }
    
    public AgeRangeRule()
    {
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int age = 0;

        try
        {
            if (((string)value).Length > 0)
                age = Int32.Parse((String)value);
        }
        catch (Exception e)
        {
            return new ValidationResult(false, $"Illegal characters or {e.Message}");
        }

        if ((age < Min) || (age > Max))
        {
            return new ValidationResult(false,
              $"Please enter an age in the range: {Min}-{Max}.");
        }
        return ValidationResult.ValidResult;
    }
}

注解

创建类的子类时,必须实现此方法 ValidationRule ,以便创建自定义验证规则。You must implement this method when you create a subclass of the ValidationRule class in order to create a custom validation rule.

数据绑定引擎每次将值从绑定目标属性传输 (从用户输入) 到绑定源属性 (基础数据) 时,它将检查是否 ValidationRules 为该绑定定义了任何。Each time the data binding engine transfers a value from the binding target property (from user input) to the binding source property (the underlying data), it checks whether any ValidationRules are defined for that binding. 如果为 ValidationRules 绑定定义了,则引擎将 Validate 在每个上调用方法, ValidationRule 直到其中一个发现错误或直到所有它们都通过。If ValidationRules are defined for the binding, the engine calls the Validate method on each ValidationRule until one of them finds an error or until all of them pass.

有关数据验证的详细信息,请参阅 数据绑定概述For detailed information about data validation, see Data Binding Overview.

另请参阅

适用于

Validate(Object, CultureInfo, BindingExpressionBase)

对值执行验证检查。Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingExpressionBase ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingExpressionBase owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingExpressionBase) As ValidationResult

参数

value
Object

要检查的来自绑定目标的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在此规则中使用的区域性。The culture to use in this rule.

owner
BindingExpressionBase

使用测试规则的绑定表达式。The binding expression that uses the validation rule.

返回

ValidationResult

ValidationResult 对象。A ValidationResult object.

适用于

Validate(Object, CultureInfo, BindingGroup)

对值执行验证检查。Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingGroup ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingGroup owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingGroup) As ValidationResult

参数

value
Object

要检查的来自绑定目标的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在此规则中使用的区域性。The culture to use in this rule.

owner
BindingGroup

使用测试规则的绑定组。The binding group that uses the validation rule.

返回

ValidationResult

ValidationResult 对象。A ValidationResult object.

适用于