Question

I'm trying to apply a trigger to a base WPF window I made. I can successfully apply styles to it but the trigger doesn't seem to work.

Essentially what I'm doing is at runtime I'm dumping the current resource dictionaries in the application and loading in another set of resource dictionaries to account for high contrast. It seems to be working, but I'm trying to work out a solution of images that need to be handled in high contrast mode. In this particular case the Window's background is set to an ImageBrush. I'm trying to use a trigger to set the background to a system color when in high contrast mode.

I tried this solution but neither seem to work:

<Style TargetType="{x:Type local:Mywindow}">
  <Setter Property="Tag"
          Value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}" />
  <Style.Triggers>
    <Trigger
        Property="Tag"
        Value="True">
      <Setter Property="Background" Value="{DynamicResource MyColor}"/>
    </Trigger>
  </Style.Triggers>
</Style>

and also

<Style TargetType="{x:Type local:Mywindow}">
    <Style.Triggers>
        <DataTrigger
            Binding="{Binding Source={x:Static SystemParameters.HighContrast}}"
            Value="True">
            <Setter Property="Style" Value="{DynamicResource highContrastStyle}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
Was it helpful?

Solution

Figured it out. Had to do the binding to RelativeSource.Self.. Add two triggers for true and false. Ignore the converter.. I needed one for the image I was using.

<Style x:Key="MyWindowKey" TargetType="{x:Type test:MyWindow}">
        <Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource= {x:Static RelativeSource.Self}, Path=Tag}" Value="True">
                <Setter Property="Background" Value="{DynamicResource MyWindowBrush}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource= {x:Static RelativeSource.Self},  Path=Tag}" Value="False">
                <Setter Property="Background">
                    <Setter.Value>
                        <ImageBrush ImageSource="{Binding Source={x:Static test:Images.MyImage},Converter={StaticResource ResourceKey=ImageConverter}}" Stretch="UniformToFill">
                        </ImageBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style><
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top