Click here to Skip to main content
15,029,206 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 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 i found on here: Neither seem to work.

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

and also

<Style TargetType="{x:Type local:Mywindow}">
               Binding="{Binding Source={x:Static SystemParameters.HighContrast}}"
               <Setter Property="Style" Value="{DynamicResource highContrastStyle}"/>

1 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}" xmlns:x="#unknown">
        <setter property="Tag" value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}" />
            <datatrigger binding="{Binding RelativeSource= {x:Static RelativeSource.Self}, Path=Tag}" value="True">
                <setter property="Background" value="{DynamicResource MyWindowBrush}" />
            <datatrigger binding="{Binding RelativeSource= {x:Static RelativeSource.Self},  Path=Tag}" value="False">
                <setter property="Background">
                        <imagebrush imagesource="{Binding Source={x:Static test:Images.MyImage},Converter={StaticResource ResourceKey=ImageConverter}}" stretch="UniformToFill">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900