Reputation: 510
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>
Upvotes: 0
Views: 289
Reputation: 5785
I haven't tested this code out, but perhaps you could use a MultiTrigger
and bind to the HighContrast
property of the SystemParameters
object. Something like this:
[removed, see below]
Note: I'm pretty sure the HighContrast
property of the static SystemParameters
class is not a dependency property, and won't raise NotifyPropertyChanged
, 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.
<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}"/>
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ServerName.HasErrorsToShow}" Value="true"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
Upvotes: 2