Reputation: 69
I created a custom ControlTemplate
for my TextBoxes
and I can't manage to override the default behaviour of the ValidationRule errors.
The TextBox
border just turn red and i can't find where to override this.
The thing i'd like to to in my ControlTemplate
is something like this :
<EventSetter Event="HasError" Handler="TextBox_HasErrors"/>
And in my code behind :
private void TextBox_HasErrors(...)
{
//Change few things in my TextBox
}
As I Overrided the default ControlTemplate
of the TextBox
, i have this ScrollViewer x:Name="PART_ContentHost"
which I think is responsible for the border coloration, but i don't know how and where to change that
Actually my usage of the textBox is like this :
<TextBox Tag="Email">
<TextBox.Text>
<Binding Path="Email" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:TextBoxEmailValidationRule Domain=".com"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Does some one know how i could change the red border behavior ?
Upvotes: 1
Views: 500
Reputation: 169160
The red border is defined in the default Validation.ErrorTemplate
of the control. You can easily create your own error template by setting the attached property to a custom ControlTemplate
:
<TextBox Tag="Email">
<TextBox.Text>
<Binding Path="Email" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:TextBoxEmailValidationRule Domain=".com"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel>
<!-- Placeholder for the TextBox itself -->
<AdornedElementPlaceholder x:Name="textBox"/>
<TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/>
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
Please refer to this blog post for more information about this and data validation in WPF in general.
Upvotes: 3