I have a textbox that a user puts a server name into, and it’s validated as an FQDN as he types. I also have a style that gets applied when the validation fails that makes the background of the textbox pink. However, I don’t want this to happen when high contrast mode is on and I can’t really seem to find much literature on how to accomplish this.
Here’s the textbox/style:
<TextBox ...>
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ServerName.HasErrorsToShow}" Value="true">
<Setter Property="TextBox.Background" Value="Pink" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
How can I accomplish this? Thanks!
EDIT 1:
I tried using a MultiTrigger. It seems promising, but I can’t quite get it right. This chunk of code worked (for testing whether I could base decisions on high contrast):
<Style.Triggers>
<DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="True">
<Setter Property="TextBox.Background" Value="Pink" />
</DataTrigger>
</Style.Triggers>
But when I tried adding the MultiTrigger, I got a “Set property ‘System.Windows.FrameworkElement.Style’ threw an exception” exception. The inner exception was “Must have non-null value for ‘Property'”. Here’s the code for that:
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding ServerName.HasErrorsToShow}" Value="True" />
<Condition Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="TextBox.Background" Value="Pink" />
</MultiTrigger>
</Style.Triggers>
I haven’t tested this code out, but perhaps you could use a
MultiTriggerand bind to theHighContrastproperty of theSystemParametersobject. Something like this:[removed, see below]
Note: I’m pretty sure the
HighContrastproperty of the staticSystemParametersclass is not a dependency property, and won’t raiseNotifyPropertyChanged, so if the user changes the setting while the application is already open, I don’t believe the trigger will fire.Edit:
This style seems to do the job for me.