ValidationError 类

定义

表示一个验证错误,该错误在 ValidationRule 报告验证错误时由绑定引擎创建,或者通过 MarkInvalid(BindingExpressionBase, ValidationError) 方法显式创建。Represents a validation error that is created either by the binding engine when a ValidationRule reports a validation error, or through the MarkInvalid(BindingExpressionBase, ValidationError) method explicitly.

public ref class ValidationError
public class ValidationError
type ValidationError = class
Public Class ValidationError
继承
ValidationError

示例

下面的示例演示了一个创建的、 ToolTip 用于报告验证错误消息的样式触发器。The following example shows a style trigger that creates a ToolTip that reports a validation error message. Validation.Errors附加属性返回 ValidationError 绑定元素上的对象的集合。The Validation.Errors attached property returns a collection of ValidationError objects on the bound element.

<Style x:Key="textStyleTextBox" TargetType="TextBox">
  <Setter Property="Foreground" Value="#333333" />
  <Setter Property="MaxLength" Value="40" />
  <Setter Property="Width" Value="392" />
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

注解

WPF 数据绑定模型使您能够将验证规则与您的 Binding 对象相关联。The WPF data binding model enables you to associate validation rules with your Binding object. 在调用转换器之前从目标到源的值传输过程中进行验证。Validation occurs during target-to-source value transfer before the converter is called. 下面描述了验证过程。The following describes the validation process.

  1. 当值从目标属性传输到源属性时,数据绑定引擎首先 ValidationError 会删除可能已添加到 Validation.Errors 绑定元素的附加属性的任何。When a value is being transferred from the target property to the source property, the data binding engine first removes any ValidationError that might have been added to the Validation.Errors attached property of the bound element. 然后,它会检查是否定义了任何自定义验证规则 Binding ,在这种情况下,它会 Validate 在每个规则上调用方法, ValidationRule 直到其中一个发生故障或直到全部通过为止。It then checks whether any custom validation rules are defined for that Binding, in which case it calls the Validate method on each ValidationRule until one of them fails or until all of them pass.

  2. 如果有一个没有通过的自定义规则,则绑定引擎将创建一个 ValidationError 对象并将其添加到 Validation.Errors 绑定元素的集合中。Once there is a custom rule that does not pass, the binding engine creates a ValidationError object and adds it to the Validation.Errors collection of the bound element. 如果 Validation.Errors 不为空,则元素的 Validation.HasError 附加属性设置为 trueWhen Validation.Errors is not empty, the Validation.HasError attached property of the element is set to true. 此外,如果 BindingNotifyOnValidationError 属性设置为 true,则绑定引擎将在元素上引发 Validation.Error 附加事件。Also, if the NotifyOnValidationError property of the Binding is set to true, then the binding engine raises the Validation.Error attached event on the element.

  3. 如果所有规则都通过,则绑定引擎会调用转换器(如果存在)。If all the rules pass, the binding engine then calls the converter, if one exists.

  4. 如果转换器通过,则绑定引擎将调用源属性的 setter。If the converter passes, the binding engine calls the setter of the source property.

  5. 如果绑定具有 ExceptionValidationRule 与之关联的,并且在步骤3或4中引发了异常,则绑定引擎会检查是否存在 UpdateSourceExceptionFilterIf the binding has an ExceptionValidationRule associated with it and an exception is thrown during step 3 or 4, the binding engine checks to see if there is an UpdateSourceExceptionFilter. 你可以使用 UpdateSourceExceptionFilter 回调提供自定义处理程序来处理异常。You can use the UpdateSourceExceptionFilter callback to provide a custom handler for handling exceptions. 如果未在 Binding 上指定 UpdateSourceExceptionFilter,则绑定引擎会创建具有异常的 ValidationError 并将其添加到绑定元素的 Validation.Errors 集合中。If an UpdateSourceExceptionFilter is not specified on the Binding, the binding engine creates a ValidationError with the exception and adds it to the Validation.Errors collection of the bound element.

(目标到源或源到目标) 方向的有效值传输将清除 Validation.Errors 附加属性。A valid value transfer in either direction (target to source or source to target) will clear the Validation.Errors attached property.

构造函数

ValidationError(ValidationRule, Object)

使用指定的参数初始化 ValidationError 类的新实例。Initializes a new instance of the ValidationError class with the specified parameters.

ValidationError(ValidationRule, Object, Object, Exception)

使用指定的参数初始化 ValidationError 类的新实例。Initializes a new instance of the ValidationError class with the specified parameters.

属性

BindingInError

获取此 BindingExpressionMultiBindingExpressionValidationError对象。Gets the BindingExpression or MultiBindingExpression object of this ValidationError. 该对象或者被显式标记为无效,或者具有失败的验证规则。The object is either marked invalid explicitly or has a failed validation rule.

ErrorContent

获取或设置一个对象,该对象为此 ValidationError 提供其他上下文,如描述错误的字符串。Gets or sets an object that provides additional context for this ValidationError, such as a string describing the error.

Exception

获取或设置导致此 ExceptionValidationError 对象(如果此错误是异常造成的)。Gets or sets the Exception object that was the cause of this ValidationError, if the error is the result of an exception.

RuleInError

获取或设置导致此 ValidationRuleValidationError 对象(如果此错误是验证规则造成的)。Gets or sets the ValidationRule object that was the cause of this ValidationError, if the error is the result of a validation rule.

方法

Equals(Object)

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

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

另请参阅