ValidationResult.IsValid 屬性

定義

取得值,這個值表示針對 ValidationRule 選取的值是否有效。Gets a value that indicates whether the value checked against the ValidationRule is valid.

public:
 property bool IsValid { bool get(); };
public bool IsValid { get; }
member this.IsValid : bool
Public ReadOnly Property IsValid As Boolean

屬性值

如果名稱有效則為 true,否則為 falsetrue if the value is valid; otherwise, false. 預設值是 falseThe default value is false.

範例

下列範例顯示驗證規則的執行,如果輸入值包含非數位字元或在下限和上限外,則將其標示為無效。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. 如果值無效,則會將所傳回 ValidationResultErrorContent 屬性和 IsValid 屬性分別設定為適當的錯誤訊息和 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;

        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;
    }
}

備註

WPFWPF 資料系結模型可讓您將 ValidationRules 與您的 BindingMultiBinding 物件產生關聯。The WPFWPF data binding model enables you to associate ValidationRules with your Binding or MultiBinding object. 您可以建立自訂規則,方法是將 ValidationRule 類別子類別化,並執行 Validate 方法。You 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.

適用於

另請參閱