I am creating a custom text box control to have some default formatting and validation functionality, including a custom validation property which accepts an Action or custom function.
In my current situation I can't use the Attribute validation framework that some people have posted, since I don't quite have access to modify the data model objects. I also cannot use ValidationRules because not all textboxes that require these validations will be bound.
I have used the generic textbox style in the static constructor rather than define my own, but I added some data triggers to set the border and tooltip based on my custom IsValid dependency property.
Everything seems to be working well, but the problem is when I mouse over or click on a textbox that has failed validation, the "invalid" style disappears and it goes to the default textbox style.
I have tried to create some additional datatriggers for IsMouseOver, IsFocused and IsMouseCaptured event (shown below) to no avail.
Am I missing something?
The Static Constructor (showing I'm using the TextBox style:
static ValidatorTextBox()
{
//Commenting this line out to use the default textbox style
DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidatorTextBox), new FrameworkPropertyMetadata(typeof(TextBox)));
}
And here's my style:
<Style TargetType="{x:Type local:ValidatorTextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsValid}" Value="False">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsRequired}" Value="True">
<Setter Property="Background" Value="AliceBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsFocused}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseCaptured}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>