2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:c="clr-namespace:BindValidation_VB"
6 Title="Binding Validation Sample"
7 SizeToContent="WidthAndHeight"
11 <c:MyDataSource x:Key="ods"/>
13 <ControlTemplate x:Key="validationTemplate">
15 <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
16 <AdornedElementPlaceholder/>
20 <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
22 <Trigger Property="Validation.HasError" Value="true">
23 <Setter Property="ToolTip"
24 Value="{Binding RelativeSource={x:Static RelativeSource.Self},
25 Path=(Validation.Errors)[0].ErrorContent}"/>
32 <Grid.ColumnDefinitions>
36 </Grid.ColumnDefinitions>
43 </Grid.RowDefinitions>
46 Grid.Row="0" Grid.ColumnSpan="2"
47 FontSize="20" Margin="8"
48 Text="Enter a number between 21-130 or there will be a validation error:"/>
50 <Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
51 Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
52 <TextBox Name="textBox1" Width="50" FontSize="15"
53 Validation.ErrorTemplate="{StaticResource validationTemplate}"
54 Style="{StaticResource textBoxInError}"
55 Grid.Row="1" Grid.Column="1" Margin="2">
57 <Binding Path="Age" Source="{StaticResource ods}"
58 UpdateSourceTrigger="PropertyChanged" >
59 <Binding.ValidationRules>
60 <c:AgeRangeRule Min="21" Max="130"/>
61 </Binding.ValidationRules>
66 <Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
67 Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
68 <TextBox Name="textBox2" Width="50" FontSize="15"
69 Grid.Row="2" Grid.Column="1" Margin="2">
71 <Binding Path="Age2" Source="{StaticResource ods}"
72 UpdateSourceTrigger="PropertyChanged" >
73 <Binding.ValidationRules>
74 <c:AgeRangeRule Min="21" Max="130"/>
75 </Binding.ValidationRules>
80 <TextBlock Grid.Row="3" Grid.ColumnSpan="3" FontSize="20" Margin="8"
81 Text="The following TextBox uses the ExceptionValidationRule and UpdateSourceExceptionFilter handler:"/>
82 <Label Grid.Row="4" Grid.Column="0" FontSize="15" Margin="2"
83 Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>
84 <TextBox Name="textBox3" Width="50" FontSize="15"
85 Grid.Row="4" Grid.Column="1" Margin="2"
86 Validation.ErrorTemplate="{StaticResource validationTemplate}"
87 Style="{StaticResource textBoxInError}">
89 <Binding Path="Age3" Source="{StaticResource ods}"
90 UpdateSourceTrigger="PropertyChanged">
91 <Binding.ValidationRules>
92 <ExceptionValidationRule/>
93 </Binding.ValidationRules>
97 <CheckBox Name="cb" FontSize="15" HorizontalAlignment="Left"
98 Grid.Row="4" Grid.Column="2" Margin="5"
99 Checked="UseCustomHandler" Unchecked="DisableCustomHandler">Enable Custom Handler (see ToolTip)</CheckBox>