ValidationError Class
Collapse the table of content
Expand the table of content

ValidationError Class


Represents a validation error that is created either by the binding engine when a ValidationRule reports a validation error, or through the MarkInvalid method explicitly.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)


Public Class ValidationError

System_CAPS_pubmethodValidationError(ValidationRule, Object)

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

System_CAPS_pubmethodValidationError(ValidationRule, Object, Object, Exception)

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


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


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


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


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


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

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.

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" />
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},

.NET Framework
Available since 3.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft