ValidationResult.ErrorContent 属性


获取提供有关无效性的附加信息的对象。Gets an object that provides additional information about the invalidity.

 property System::Object ^ ErrorContent { System::Object ^ get(); };
public object ErrorContent { get; }
member this.ErrorContent : obj
Public ReadOnly Property ErrorContent As Object



一个提供有关无效性的附加信息的对象。An object that provides additional information about the invalidity.


下面的示例演示如何实现验证规则,该规则将输入值标记为无效(如果它包含非数字字符或超出下限和上限)。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. 如果值无效,则返回的 ErrorContent 属性和 IsValid 属性将 ValidationResult 分别设置为相应的错误消息 falseIf 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;

            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;


利用 WPF 数据绑定模型,您可以 ValidationRulesBinding 或对象关联 MultiBindingThe WPF data binding model enables you to associate ValidationRules with your Binding or MultiBinding object. 可以通过为类创建子类 ValidationRule 并实现方法来创建自定义规则 ValidateYou can create custom rules by subclassing the ValidationRule class and implementing the Validate method. Validate方法返回一个 ValidationResult 对象,用于报告选中值是否有效。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.