本文发表在 rolia.net 枫下论坛<Window x:Class="BindValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:BindValidation"
Title="Binding Validation Sample"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize">
<Window.Resources>
<c:MyDataSource x:Key="ods"/>
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type 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>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0" Grid.ColumnSpan="2"
FontSize="20" Margin="8"
Text="Enter a number between 21-130 or there will be a validation error:"/>
<Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
<TextBox Name="textBox2" Width="50" FontSize="15"
Grid.Row="2" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age2" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Grid.Row="3" Grid.ColumnSpan="3" FontSize="20" Margin="8"
Text="The following TextBox uses the ExceptionValidationRule and UpdateSourceExceptionFilter handler:"/>
<TextBlock Grid.Row="4" Grid.ColumnSpan="3" FontSize="15" Margin="8"
Text="Enter a non-numeric value to raise the exception."/>
<Label Grid.Row="5" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>
<TextBox Name="textBox3" Width="50" FontSize="15"
Grid.Row="5" Grid.Column="1" Margin="2"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Age3" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<CheckBox Name="cb" FontSize="15" HorizontalAlignment="Left"
Grid.Row="5" Grid.Column="2" Margin="5" IsChecked="True"
Checked="UseCustomHandler" Unchecked="DisableCustomHandler">Enable Custom Handler (see ToolTip)</CheckBox>
</Grid>
</Window>更多精彩文章及讨论,请光临枫下论坛 rolia.net
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:BindValidation"
Title="Binding Validation Sample"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize">
<Window.Resources>
<c:MyDataSource x:Key="ods"/>
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type 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>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0" Grid.ColumnSpan="2"
FontSize="20" Margin="8"
Text="Enter a number between 21-130 or there will be a validation error:"/>
<Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
<TextBox Name="textBox2" Width="50" FontSize="15"
Grid.Row="2" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age2" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Grid.Row="3" Grid.ColumnSpan="3" FontSize="20" Margin="8"
Text="The following TextBox uses the ExceptionValidationRule and UpdateSourceExceptionFilter handler:"/>
<TextBlock Grid.Row="4" Grid.ColumnSpan="3" FontSize="15" Margin="8"
Text="Enter a non-numeric value to raise the exception."/>
<Label Grid.Row="5" Grid.Column="0" FontSize="15" Margin="2"
Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>
<TextBox Name="textBox3" Width="50" FontSize="15"
Grid.Row="5" Grid.Column="1" Margin="2"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Age3" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<CheckBox Name="cb" FontSize="15" HorizontalAlignment="Left"
Grid.Row="5" Grid.Column="2" Margin="5" IsChecked="True"
Checked="UseCustomHandler" Unchecked="DisableCustomHandler">Enable Custom Handler (see ToolTip)</CheckBox>
</Grid>
</Window>更多精彩文章及讨论,请光临枫下论坛 rolia.net