ValidationRule 類別

定義

提供建立自訂規則的方式,以檢查使用者輸入的有效性。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
繼承
ValidationRule
衍生

範例

下列範例顯示如何執行驗證規則。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. 如果傳回ValidationResult的值無效, 則ErrorContent會將屬性設定為IsValid適當的錯誤訊息, 並將屬性設定為falseIf 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;
    }
}

備註

當您使用WPFWPF資料系結模型時, 可以與ValidationRules系結物件產生關聯。When you use the WPFWPF data binding model, you can associate ValidationRules with your binding object. 若要建立自訂規則, 請建立此類別的子類別Validate , 並執行方法。To create custom rules, make a subclass of this class and implement the Validate method. (選擇性) 使用內ExceptionValidationRule建的, 它會攔截來源更新期間所擲回的例外狀況, DataErrorValidationRule或是, 它會檢查來源物件IDataErrorInfo的執行所引發的錯誤。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.

系結引擎每次ValidationRule將輸入值 (系結目標屬性值) 傳送到系結來源屬性時, 都會檢查與系結相關聯的每一個。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.

建構函式

ValidationRule()

初始化 ValidationRule 類別的新執行個體。Initializes a new instance of the ValidationRule class.

ValidationRule(ValidationStep, Boolean)

使用指定的驗證步驟以及表示目標更新時是否執行驗證規則的值,初始化 ValidationRule 類別的新執行個體。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.

屬性

ValidatesOnTargetUpdated

取得或設定值,這個值表示是否在 Binding 的目標更新值執行驗證規則。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.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 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.

適用於

另請參閱