Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ValidationResult Class

Represents the result returned by the ValidationRule.Validate method that indicates whether the checked value passed the ValidationRule.

System.Object
  System.Windows.Controls.ValidationResult

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

The ValidationResult type exposes the following members.

  NameDescription
Public methodValidationResultInitializes a new instance of the ValidationResult class.
Top
  NameDescription
Public propertyErrorContentGets an object that provides additional information about the invalidity.
Public propertyIsValidGets a value that indicates whether the value checked against the ValidationRule is valid.
Public propertyStatic memberValidResultGets a valid instance of ValidationResult.
Top
  NameDescription
Public methodEqualsCompares the specified instance and the current instance of ValidationResult for value equality. (Overrides Object.Equals(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 methodGetHashCodeReturns the hash code for this ValidationResult. (Overrides Object.GetHashCode().)
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
  NameDescription
Public operatorStatic memberEqualityCompares two ValidationResult objects for value equality.
Public operatorStatic memberInequalityCompares two ValidationResult objects for value inequality.
Top

The WPF data binding model enables you to associate ValidationRules with your Binding or MultiBinding object. You can create custom rules by subclassing the ValidationRule class and implementing the Validate method. The Validate method returns a ValidationResult object to report whether the checked value is valid.

For a detailed discussion of the validation process, see "Data Validation" in Data Binding Overview.

The following example shows the implementation of a validation rule that marks the input value as invalid if it contains non-numeric characters or outside the lower and upper bounds. If the value is invalid, the ErrorContent property and the IsValid property of the returned ValidationResult are set to the appropriate error message and false respectively.

For the complete example, see How to: Implement Binding Validation.

public class AgeRangeRule : ValidationRule
{
    private int _min;
    private int _max;

    public AgeRangeRule()
    {
    }

    public int Min
    {
        get { return _min; }
        set { _min = value; }
    }

    public int Max
    {
        get { return _max; }
        set { _max = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int age = 0;

        try
        {
            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 + ".");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, 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. All rights reserved.