System.Windows.Controls Nam ...


.NET Framework Class Library
ValidationRule Class

Updated: July 2008

Provides a way to create a custom rule in order to check the validity of user input.

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

Visual Basic (Declaration)
Public MustInherit Class ValidationRule
Visual Basic (Usage)
Dim instance As ValidationRule
C#
public abstract class ValidationRule
Visual C++
public ref class ValidationRule abstract
JScript
public abstract class ValidationRule
XAML
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.
Remarks

When you use the WPF data binding model, you can associate ValidationRules with your binding object. To create custom rules, make a subclass of this class and implement the Validate method. Optionally, use the built-in ExceptionValidationRule, which catches exceptions that are thrown during source updates, or the DataErrorValidationRule, which checks for errors raised by the IDataErrorInfo implementation of the source object.

The binding engine checks each ValidationRule that is associated with a binding every time it transfers an input value, which is the binding target property value, to the binding source property.

For detailed information about data validation, see Data Binding Overview.

For information about how to validate user-provided data in a dialog box, see Dialog Boxes Overview.

Examples

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

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

C#
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);
        }
    }
}
Inheritance Hierarchy

System..::.Object
  System.Windows.Controls..::.ValidationRule
    System.Windows.Controls..::.ExceptionValidationRule
Thread Safety

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Change History

Date

History

Reason

July 2008

Added new members: ValidatesOnTargetUpdated property, ValidationStep property, ValidationRule(ValidationStep, Boolean) constructor.

SP1 feature change.

Tags :


Community Content

Szymon Kobalczyk
Don't use exceptions to catch number formating errors

As a comment to the AgeRangeRule shown in the example: Relaying on exception handling to catch number formating errors is a bad practice. Use Int32.TryParse instead.

Tags :

Page view tracker