Edit

Share via


ValidationRule Class

Definition

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

public ref class ValidationRule abstract
public abstract class ValidationRule
type ValidationRule = class
Public MustInherit Class ValidationRule
Inheritance
ValidationRule
Derived

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.

public class AgeRangeRule : ValidationRule
{
    public int Min { get; set; }
    public int Max { get; set; }
    
    public AgeRangeRule()
    {
    }

    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}.");
        }
        return ValidationResult.ValidResult;
    }
}

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.

Constructors

ValidationRule()

Initializes a new instance of the ValidationRule class.

ValidationRule(ValidationStep, Boolean)

Initializes a new instance of the ValidationRule class with the specified validation step and a value that indicates whether the validation rule runs when the target is updated.

Properties

ValidatesOnTargetUpdated

Gets or sets a value that indicates whether the validation rule runs when the target of the Binding is updated.

ValidationStep

Gets or sets when the validation rule runs.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
Validate(Object, CultureInfo)

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

Validate(Object, CultureInfo, BindingExpressionBase)

Performs validation checks on a value.

Validate(Object, CultureInfo, BindingGroup)

Performs validation checks on a value.

Applies to

See also