Binding.ValidatesOnExceptions Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau menetapkan nilai yang menunjukkan apakah akan menyertakan ExceptionValidationRule.
public:
property bool ValidatesOnExceptions { bool get(); void set(bool value); };
public bool ValidatesOnExceptions { get; set; }
member this.ValidatesOnExceptions : bool with get, set
Public Property ValidatesOnExceptions As Boolean
Nilai Properti
true untuk menyertakan ExceptionValidationRule; jika tidak, false.
Contoh
Contoh berikut digunakan ValidatesOnExceptions untuk memvalidasi input pengguna dalam TextBox. Contoh pertama membuat jenis data yang melemparkan pengecualian saat Age properti diatur ke properti yang tidak valid.
public class PersonThrowException
{
private int age;
public int Age
{
get { return age; }
set
{
if (value < 0 || value > 150)
{
throw new ArgumentException("Age must not be less than 0 or greater than 150.");
}
age = value;
}
}
}
Public Class PersonThrowException
Private m_age As Integer
Public Property Age() As Integer
Get
Return m_age
End Get
Set(ByVal value As Integer)
If value < 0 OrElse value > 150 Then
Throw New ArgumentException("Age must not be less than 0 or greater than 150.")
End If
m_age = value
End Set
End Property
End Class
Contoh berikut mengikat Age properti ke TextBox dan diatur ValidatesOnExceptions ke true pada Binding. Saat pengguna memasukkan nilai yang tidak valid, batas merah muncul di TextBox dan ToolTip melaporkan pesan kesalahan.
<StackPanel Margin="20">
<StackPanel.Resources>
<src:PersonThrowException x:Key="data"/>
<!--The tool tip for the TextBox to display the validation error message.-->
<Style x:Key="textBoxInError" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock>Enter your age:</TextBlock>
<TextBox Style="{StaticResource textBoxInError}">
<TextBox.Text>
<!--By setting ValidatesOnExceptions to True, it checks for exceptions
that are thrown during the update of the source property.
An alternative syntax is to add <ExceptionValidationRule/> within
the <Binding.ValidationRules> section.-->
<Binding Path="Age" Source="{StaticResource data}"
ValidatesOnExceptions="True"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock>Mouse-over to see the validation error message.</TextBlock>
</StackPanel>
Keterangan
Mengatur properti ini menyediakan alternatif untuk menggunakan ExceptionValidationRule elemen secara eksplisit. ExceptionValidationRule adalah aturan validasi bawaan yang memeriksa pengecualian yang dilemparkan selama pembaruan properti sumber. Jika pengecualian dilemparkan, mesin pengikatan membuat ValidationError dengan pengecualian dan menambahkannya ke Validation.Errors koleksi elemen terikat. Kurangnya kesalahan menghapus umpan balik validasi ini, kecuali aturan lain menimbulkan masalah validasi.
ValidatesOnExceptions diperkenalkan dalam .NET Framework versi 3.5. Untuk informasi selengkapnya, lihat Versi dan Dependensi .NET Framework.