ValidationResult.ErrorContent 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得一個提供關於無效性的額外資訊的物件。
public:
property System::Object ^ ErrorContent { System::Object ^ get(); };
public object ErrorContent { get; }
member this.ErrorContent : obj
Public ReadOnly Property ErrorContent As Object
屬性值
一個提供關於無效性額外資訊的物件。
範例
以下範例展示了一項驗證規則的實作,該規則將輸入值標記為無效,若輸入包含非數字字元或超出上下界。 若值無效,則ErrorContent將回傳ValidationResult的屬性IsValid與屬性分別設為適當的錯誤訊息 和false。
完整範例請參見 《如何:實作綁定驗證》。
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;
}
}
備註
WPF 資料綁定模型讓你能與你的 Binding or MultiBinding 物件產生關聯ValidationRules。 你可以透過子 ValidationRule 類別化類別並實作 Validate 方法來建立自訂規則。 該方法回 Validate 傳一個 ValidationResult 物件,用以回報檢查值是否有效。
關於驗證流程的詳細討論,請參閱 資料綁定概述中的「資料驗證」。