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: http://social.msdn.microsoft.com/Forums/en/wpf/thread/ba9a379b-4f02-408c-954b-6057e6d0a1b5. Neither seem to work.

C#
<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

C#
<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>
Posted

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.

C#
<style x:key="MyWindowKey" targettype="{x:Type test:MyWindow}" xmlns:x="#unknown">
        <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>
   

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