Windows apps
Collapse the table of content
Expand the table of content
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.

ValidationRule.Validate Method

When overridden in a derived class, performs validation checks on a value.

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

public abstract ValidationResult Validate(
	Object value,
	CultureInfo cultureInfo


Type: System.Object
The value from the binding target to check.
Type: System.Globalization.CultureInfo
The culture to use in this rule.

You must implement this method when you create a subclass of the ValidationRule class in order to create a custom validation rule.

Each time the data binding engine transfers a value from the binding target property (from user input) to the binding source property (the underlying data), it checks whether any ValidationRules are defined for that binding. If ValidationRules are defined for the binding, the engine calls the Validate method on each ValidationRule until one of them finds an error or until all of them pass.

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

The following example shows how to implement a validation rule. In the following example, the input value is invalid if it contains non-numeric characters or if it is outside the lower and upper bounds. If the input value 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.

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;

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

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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