Bewerken

Delen via


ValidationRule.Validate Method

Definition

Performs validation checks on a value.

Overloads

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.

Validate(Object, CultureInfo)

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

public:
 abstract System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo);
public abstract System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo);
abstract member Validate : obj * System.Globalization.CultureInfo -> System.Windows.Controls.ValidationResult
Public MustOverride Function Validate (value As Object, cultureInfo As CultureInfo) As ValidationResult

Parameters

value
Object

The value from the binding target to check.

cultureInfo
CultureInfo

The culture to use in this rule.

Returns

A ValidationResult object.

Examples

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
{
    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

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.

See also

Applies to

Validate(Object, CultureInfo, BindingExpressionBase)

Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingExpressionBase ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingExpressionBase owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingExpressionBase) As ValidationResult

Parameters

value
Object

The value from the binding target to check.

cultureInfo
CultureInfo

The culture to use in this rule.

owner
BindingExpressionBase

The binding expression that uses the validation rule.

Returns

A ValidationResult object.

Applies to

Validate(Object, CultureInfo, BindingGroup)

Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingGroup ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingGroup owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingGroup) As ValidationResult

Parameters

value
Object

The value from the binding target to check.

cultureInfo
CultureInfo

The culture to use in this rule.

owner
BindingGroup

The binding group that uses the validation rule.

Returns

A ValidationResult object.

Applies to