Επεξεργασία

Κοινή χρήση μέσω


ValidationError Class

Definition

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
Inheritance
ValidationError

Examples

The following example shows a style trigger that creates a ToolTip that reports a validation error message. 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>

Remarks

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. 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. 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. 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. When Validation.Errors is not empty, the Validation.HasError attached property of the element is set to true. 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. If the converter passes, the binding engine calls the setter of the source property.

  5. If 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. You can use the UpdateSourceExceptionFilter callback to provide a custom handler for handling exceptions. 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.

A valid value transfer in either direction (target to source or source to target) will clear the Validation.Errors attached property.

Constructors

ValidationError(ValidationRule, Object, Object, Exception)

Initializes a new instance of the ValidationError class with the specified parameters.

ValidationError(ValidationRule, Object)

Initializes a new instance of the ValidationError class with the specified parameters.

Properties

BindingInError

Gets the BindingExpression or MultiBindingExpression object of this ValidationError. The object is either marked invalid explicitly or has a failed validation rule.

ErrorContent

Gets or sets an object that provides additional context for this ValidationError, such as a string describing the error.

Exception

Gets or sets the Exception object that was the cause of this ValidationError, if the error is the result of an exception.

RuleInError

Gets or sets the ValidationRule object that was the cause of this ValidationError, if the error is the result of a validation rule.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also