ExceptionValidationRule 類別

定義

表示規則,這個規則會檢查繫結來源屬性更新期間所擲回的例外狀況。Represents a rule that checks for exceptions that are thrown during the update of the binding source property.

public ref class ExceptionValidationRule sealed : System::Windows::Controls::ValidationRule
public sealed class ExceptionValidationRule : System.Windows.Controls.ValidationRule
type ExceptionValidationRule = class
    inherit ValidationRule
Public NotInheritable Class ExceptionValidationRule
Inherits ValidationRule
繼承
ExceptionValidationRule

範例

ValidationRules 屬性會接受 ValidationRule 物件的集合。The ValidationRules property takes a collection of ValidationRule objects. 下列範例會使用 TextBox.Text 做為系結目標屬性,並 StartPrice (整數)做為系結來源屬性。The following example uses TextBox.Text as the binding target property and StartPrice (an integer) as the binding source property. 當使用者輸入的值無法轉換成整數時,會擲回例外狀況,導致 ExceptionValidationRule 使目標值失效。When the user enters a value that cannot be converted to an integer, an exception is thrown, which causes the ExceptionValidationRule to invalidate the target value.

<TextBox Name="StartPriceEntryForm" Grid.Row="2" Grid.Column="1"
    Style="{StaticResource textStyleTextBox}" Margin="8,5,0,5">
  <TextBox.Text>
    <Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <ExceptionValidationRule />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

根據預設,發生驗證錯誤時,TextBox 會出現紅色框線。By default, when there is a validation error, the TextBox appears with a red border. (選擇性)您可以撰寫自訂 ErrorTemplate 來指定當發生驗證錯誤時,要如何通知使用者。Optionally, you can write a custom ErrorTemplate to specify how you want to notify the user when there is a validation error.

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

如需完整範例,請參閱資料系結示範For the complete sample, see Data Binding Demo.

備註

WPFWPF 資料系結模型可讓您將 ValidationRulesBinding 物件建立關聯。The WPFWPF data binding model enables you to associate ValidationRules with a Binding object. ExceptionValidationRule 類別是一個內建規則,它會檢查系結來源屬性更新期間所擲回的例外狀況。The ExceptionValidationRule class is a built-in rule that checks for exceptions that are thrown during the updates of the binding source property. 您可以提供自訂邏輯來指定系結引擎如何使用 UpdateSourceExceptionFilterCallback來處理這些例外狀況。You can provide custom logic to specify how the binding engine handles these exceptions by using a UpdateSourceExceptionFilterCallback. 如需詳細資訊,請參閱 UpdateSourceExceptionFilterFor more information, see UpdateSourceExceptionFilter.

明確設定 ExceptionValidationRule 的替代語法,是將 ValidatesOnExceptions 屬性設定為 BindingMultiBinding 物件上的 trueAn alternative syntax to setting the ExceptionValidationRule explicitly is to set the ValidatesOnExceptions property to true on your Binding or MultiBinding object.

您可以藉由建立衍生自 ValidationRule的類別來建立自訂規則。You can create a custom rule by creating a class that derives from ValidationRule. 如需資料驗證的詳細資訊和詳細討論,請參閱資料系結總覽For more information and a detailed discussion of data validation, see Data Binding Overview.

建構函式

ExceptionValidationRule()

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

屬性

ValidatesOnTargetUpdated

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

(繼承來源 ValidationRule)
ValidationStep

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

(繼承來源 ValidationRule)

方法

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)

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

Validate(Object, CultureInfo, BindingExpressionBase)

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

(繼承來源 ValidationRule)
Validate(Object, CultureInfo, BindingGroup)

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

(繼承來源 ValidationRule)

適用於

另請參閱