ValidationRule 类


提供创建自定义规则的一个方式,旨在检查用户输入的有效性。Provides a way to create a custom rule in order to check the validity of user input.

public ref class ValidationRule abstract
public abstract class ValidationRule
type ValidationRule = class
Public MustInherit Class ValidationRule


下面的示例演示如何实现验证规则。The following example shows how to implement a validation rule. 如果输入值包含非数字字符或超出下限,则输入值无效。The input value is invalid if it contains non-numeric characters or if it is outside the lower and upper bounds. 如果返回的值 ValidationResult 无效,则将 ErrorContent 属性设置为相应的错误消息,并将 IsValid 属性设置为 falseIf the value of the returned ValidationResult 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;

            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;


使用 WPF 数据绑定模型时,可以将 ValidationRules 与绑定对象相关联。When you use the WPF data binding model, you can associate ValidationRules with your binding object. 若要创建自定义规则,请创建此类的子类并实现 Validate 方法。To create custom rules, make a subclass of this class and implement the Validate method. (可选)使用内置的 ExceptionValidationRule ,它捕获在源更新过程中引发的异常,或使用来 DataErrorValidationRule 检查由源对象的实现所引发的错误 IDataErrorInfoOptionally, use the built-in ExceptionValidationRule, which catches exceptions that are thrown during source updates, or the DataErrorValidationRule, which checks for errors raised by the IDataErrorInfo implementation of the source object.

绑定引擎 ValidationRule 每次将输入值(绑定目标属性值)传输到绑定源属性时,都会检查每个与绑定关联的。The binding engine checks each ValidationRule that is associated with a binding every time it transfers an input value, which is the binding target property value, to the binding source property.

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

有关如何在对话框中验证用户提供的数据的信息,请参阅 对话框概述For information about how to validate user-provided data in a dialog box, see Dialog Boxes Overview.



初始化 ValidationRule 类的新实例。Initializes a new instance of the ValidationRule class.

ValidationRule(ValidationStep, Boolean)

使用指定验证步骤以及指示在目标更新时是否运行验证规则的值来初始化 ValidationRule 类的新实例。Initializes a new instance of the ValidationRule class with the specified validation step and a value that indicates whether the validation rule runs when the target is updated.



获取或设置一个值,该值指示当 Binding 的目标更新时是否运行验证规则。Gets or sets a value that indicates whether the validation rule runs when the target of the Binding is updated.


获取或设置验证规则的运行时间。Gets or sets when the validation rule runs.



确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
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.