Export (0) Print
Expand All

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.

System.Object
  System.Windows.Controls.ValidationError

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

type ValidationError =  class end

The ValidationError type exposes the following members.

  NameDescription
Public methodValidationError(ValidationRule, Object)Initializes a new instance of the ValidationError class with the specified parameters.
Public methodValidationError(ValidationRule, Object, Object, Exception)Initializes a new instance of the ValidationError class with the specified parameters.
Top

  NameDescription
Public propertyBindingInErrorGets the BindingExpression or MultiBindingExpression object of this ValidationError. The object is either marked invalid explicitly or has a failed validation rule.
Public propertyErrorContentGets or sets an object that provides additional context for this ValidationError, such as a string describing the error.
Public propertyExceptionGets or sets the Exception object that was the cause of this ValidationError, if the error is the result of an exception.
Public propertyRuleInErrorGets or sets the ValidationRule object that was the cause of this ValidationError, if the error is the result of a validation rule.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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" />
  <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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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