ValidationRule.Validate 方法

定義

在值上執行驗證檢查。Performs validation checks on a value.

多載

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.

Validate(Object, CultureInfo)

在衍生類別中覆寫時,會在值上執行驗證檢查。When overridden in a derived class, performs validation checks on a value.

public:
 abstract System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo);
public abstract System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo);
abstract member Validate : obj * System.Globalization.CultureInfo -> System.Windows.Controls.ValidationResult
Public MustOverride Function Validate (value As Object, cultureInfo As CultureInfo) As ValidationResult

參數

value
Object

要檢查之繫結目標的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在這個規則中使用的文化特性。The culture to use in this rule.

傳回

範例

下列範例顯示如何執行驗證規則。The following example shows how to implement a validation rule. 在下列範例中,如果輸入值包含非數位字元,或超出下限和上限,則其為無效。In the following example, the input value is invalid if it contains non-numeric characters or if it is outside the lower and upper bounds. 如果輸入值無效,ErrorContent 屬性會設定為適當的錯誤訊息,而且 IsValid 屬性會設定為 [false]。If the input value 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;
    }
}

備註

當您建立 ValidationRule 類別的子類別,以建立自訂驗證規則時,必須執行這個方法。You must implement this method when you create a subclass of the ValidationRule class in order to create a custom validation rule.

每次資料系結引擎將值從系結目標屬性(來自使用者輸入)傳送到系結來源屬性(基礎資料)時,都會檢查是否為該系結定義了任何 ValidationRulesEach time the data binding engine transfers a value from the binding target property (from user input) to the binding source property (the underlying data), it checks whether any ValidationRules are defined for that binding. 如果 ValidationRules 是針對系結所定義,則引擎會在每個 ValidationRule 上呼叫 Validate 方法,直到其中一個找到錯誤或所有通過為止。If ValidationRules are defined for the binding, the engine calls the Validate method on each ValidationRule until one of them finds an error or until all of them pass.

如需資料驗證的詳細資訊,請參閱資料系結總覽For detailed information about data validation, see Data Binding Overview.

另請參閱

Validate(Object, CultureInfo, BindingExpressionBase)

在值上執行驗證檢查。Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingExpressionBase ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingExpressionBase owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingExpressionBase -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingExpressionBase) As ValidationResult

參數

value
Object

要檢查之繫結目標的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在這個規則中使用的文化特性。The culture to use in this rule.

owner
BindingExpressionBase

使用驗證規則的繫結運算式。The binding expression that uses the validation rule.

傳回

Validate(Object, CultureInfo, BindingGroup)

在值上執行驗證檢查。Performs validation checks on a value.

public:
 virtual System::Windows::Controls::ValidationResult ^ Validate(System::Object ^ value, System::Globalization::CultureInfo ^ cultureInfo, System::Windows::Data::BindingGroup ^ owner);
public virtual System.Windows.Controls.ValidationResult Validate (object value, System.Globalization.CultureInfo cultureInfo, System.Windows.Data.BindingGroup owner);
abstract member Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
override this.Validate : obj * System.Globalization.CultureInfo * System.Windows.Data.BindingGroup -> System.Windows.Controls.ValidationResult
Public Overridable Function Validate (value As Object, cultureInfo As CultureInfo, owner As BindingGroup) As ValidationResult

參數

value
Object

要檢查之繫結目標的值。The value from the binding target to check.

cultureInfo
CultureInfo

要在這個規則中使用的文化特性。The culture to use in this rule.

owner
BindingGroup

使用驗證規則的繫結群組。The binding group that uses the validation rule.

傳回

適用於