Bewerken

Delen via


ValidationResult(Boolean, Object) Constructor

Definition

Initializes a new instance of the ValidationResult class.

public:
 ValidationResult(bool isValid, System::Object ^ errorContent);
public ValidationResult (bool isValid, object errorContent);
new System.Windows.Controls.ValidationResult : bool * obj -> System.Windows.Controls.ValidationResult
Public Sub New (isValid As Boolean, errorContent As Object)

Parameters

isValid
Boolean

Whether or not the value checked against the ValidationRule is valid.

errorContent
Object

Information about the invalidity.

Examples

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

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.

Applies to

See also