I have bound a validation rule to my WPF TextBox in order to control what user types in.
WPF TextBox:
<TextBox Grid.Column="0"
Grid.Row="1"
IsEnabled="{Binding Path=IsEnabled}"
Margin="5,8,8,5">
<TextBox.Text>
<Binding Path="ReferenceId"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<vRules:RefValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
It works fine. When user types in something wrong, TextBox border is getting red warning user that the input is incorrect.
Furthermore, now I am trying to display the validation error message under the TextBox as well, so I bind to the TextBox below static resource:
<ResourceDictionary>
<Style x:Key="ValidationTextBoxErrorStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsEnabled}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.HasError)}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder x:Name="placeholder" />
<TextBlock FontSize="11" FontStyle="Italic" Foreground="Red" Text="{Binding [0].ErrorContent}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
And the TextBox now looks like below:
<TextBox Grid.Column="0"
Grid.Row="1"
Style="{StaticResource ValidationTextBoxErrorStyle}"
IsEnabled="{Binding Path=IsEnabled}"
Margin="5,8,8,5">
<TextBox.Text>
<Binding Path="ReferenceId"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<vRules:RefValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Now when running it, if user types in a wrong reference Id in the TextBox with an invalid format, the validation error message is correctly displayed under the TextBox, but now the TextBox border is not getting red.
So I would like both things, I mean, when value for TextBox is invalid, I want TextBox border to get RED and at the same time, to display the validation error message under it.
So, What am I doing wrong?
Note that I want TextBox to get RED and display the validation error message under it, if and only if, there is a validation error and IsEnabled property is true so as there is more than one condition I have used a MultiDataTrigger.