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


下列範例顯示如何執行驗證規則。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;

            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.

系結引擎會在每次將輸入值(系結目標屬性值)傳輸至系結來源屬性時,檢查與系結相關聯的每個 ValidationRuleThe 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 類別的新執行個體初始化。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.



取得或設定值,這個值表示是否在 Binding 的目標更新值執行驗證規則。Gets or sets a value that indicates whether the validation rule runs when the target of the Binding is updated.


取得或設定執行驗證規則的時機。Gets or sets when the validation rule runs.



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

(繼承來源 Object)

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

(繼承來源 Object)

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

(繼承來源 Object)

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

(繼承來源 Object)

傳回代表目前物件的字串。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.