ValidationError 類別

定義

表示當 ValidationRule 報告驗證錯誤時繫結引擎所建立的驗證錯誤,或透過 MarkInvalid(BindingExpressionBase, ValidationError) 方法明確建立的驗證錯誤。Represents a validation error that is created either by the binding engine when a ValidationRule reports a validation error, or through the MarkInvalid(BindingExpressionBase, ValidationError) method explicitly.

public ref class ValidationError
public class ValidationError
type ValidationError = class
Public Class ValidationError
繼承
ValidationError

範例

下列範例顯示的樣式觸發程式會建立ToolTip報告驗證錯誤訊息的。The following example shows a style trigger that creates a ToolTip that reports a validation error message. 附加屬性會傳回繫結項目上ValidationError的物件集合。 Validation.ErrorsThe Validation.Errors attached property returns a collection of ValidationError objects on the bound element.

<Style x:Key="textStyleTextBox" TargetType="TextBox">
  <Setter Property="Foreground" Value="#333333" />
  <Setter Property="MaxLength" Value="40" />
  <Setter Property="Width" Value="392" />
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

備註

資料系結模型可讓您將驗證規則與您Binding的物件產生關聯。 WPFWPFThe WPFWPF data binding model enables you to associate validation rules with your Binding object. 呼叫轉換器之前, 會在目標對來源值傳輸期間進行驗證。Validation occurs during target-to-source value transfer before the converter is called. 以下描述驗證程式。The following describes the validation process.

  1. 將值從目標屬性傳送至來源屬性時, 資料系結引擎會先移除可能已加入ValidationError Validation.Errors繫結項目之附加屬性的任何。When a value is being transferred from the target property to the source property, the data binding engine first removes any ValidationError that might have been added to the Validation.Errors attached property of the bound element. 然後, 它會檢查是否針對該Binding規則定義了任何自訂驗證規則, 在這種情況下, 它會在每個ValidationRuleValidate呼叫方法, 直到其中一個作業失敗或全部通過為止。It then checks whether any custom validation rules are defined for that Binding, in which case it calls the Validate method on each ValidationRule until one of them fails or until all of them pass.

  2. 一旦有不會傳遞的自訂規則, 系結引擎就會建立ValidationError物件, 並將它加入Validation.Errors至繫結項目的集合。Once there is a custom rule that does not pass, the binding engine creates a ValidationError object and adds it to the Validation.Errors collection of the bound element. Validation.Errors不是空的時Validation.HasError , 元素的附加屬性會設定為trueWhen Validation.Errors is not empty, the Validation.HasError attached property of the element is set to true. 此外, 如果NotifyOnValidationError的屬性Binding設定為true, 則系結引擎會在元素上引發Validation.Error附加事件。Also, if the NotifyOnValidationError property of the Binding is set to true, then the binding engine raises the Validation.Error attached event on the element.

  3. 如果所有規則都通過, 系結引擎就會呼叫轉換器 (如果有的話)。If all the rules pass, the binding engine then calls the converter, if one exists.

  4. 如果轉換器通過, 系結引擎會呼叫 source 屬性的 setter。If the converter passes, the binding engine calls the setter of the source property.

  5. 如果系結具有與ExceptionValidationRule它相關聯的, 而且在步驟3或4中擲回例外狀況, 則系結引擎UpdateSourceExceptionFilter會檢查是否有。If the binding has an ExceptionValidationRule associated with it and an exception is thrown during step 3 or 4, the binding engine checks to see if there is an UpdateSourceExceptionFilter. 您可以使用UpdateSourceExceptionFilter回呼來提供處理例外狀況的自訂處理常式。You can use the UpdateSourceExceptionFilter callback to provide a custom handler for handling exceptions. 如果未ValidationError Validation.Errors在上指定Binding, 系結引擎會建立具有例外狀況的,並將它新增至綁定UpdateSourceExceptionFilter項的集合。If an UpdateSourceExceptionFilter is not specified on the Binding, the binding engine creates a ValidationError with the exception and adds it to the Validation.Errors collection of the bound element.

任一方向的有效值傳輸 (目標到來源或目標的來源) 都會清除Validation.Errors附加屬性。A valid value transfer in either direction (target to source or source to target) will clear the Validation.Errors attached property.

建構函式

ValidationError(ValidationRule, Object)

使用指定的參數,初始化 ValidationError 類別的新執行個體。Initializes a new instance of the ValidationError class with the specified parameters.

ValidationError(ValidationRule, Object, Object, Exception)

使用指定的參數,初始化 ValidationError 類別的新執行個體。Initializes a new instance of the ValidationError class with the specified parameters.

屬性

BindingInError

取得這個 BindingExpressionMultiBindingExpressionValidationError 物件。Gets the BindingExpression or MultiBindingExpression object of this ValidationError. 此物件可能明確標記為無效,或有失敗的驗證規則。The object is either marked invalid explicitly or has a failed validation rule.

ErrorContent

取得或設定物件,這個物件提供這個 ValidationError 的其他內容 (例如,描述錯誤的字串)。Gets or sets an object that provides additional context for this ValidationError, such as a string describing the error.

Exception

取得或設定 Exception 物件,該物件是這個 ValidationError 的原因,如果錯誤是因為例外狀況所造成。Gets or sets the Exception object that was the cause of this ValidationError, if the error is the result of an exception.

RuleInError

取得或設定 ValidationRule 物件,該物件是這個 ValidationError 的原因,如果錯誤是因為驗證規則失敗所造成。Gets or sets the ValidationRule object that was the cause of this ValidationError, if the error is the result of a validation rule.

方法

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)

適用於

另請參閱