views:

111

answers:

1

I have the following xaml:

    <DockPanel>
    <DockPanel>
        <CheckBox IsChecked="{Binding Path=Test}" />
        <CheckBox IsChecked="{Binding Path=Test}" />
    </DockPanel>
    <DockPanel DockPanel.Dock="Left" Width="10" Background="Blue">
        <DockPanel.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Test}" Value="True">
                        <Setter Property="DockPanel.Background" Value="Yellow" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DockPanel.Style>
    </DockPanel>
</DockPanel>

Now - the 2 checkboxes link properly - checking one will check the other - but the datatrigger is not firing at all.

What am I doing wrong?

+4  A: 

The issue here is Property Value Precedence.

You are currently setting the Background to blue directly on the DockPanel. This explicit property will override any value set by the trigger.

Instead, you must set the original "Background" as a setter in the style.

   <DockPanel DockPanel.Dock="Left" Width="10">
       <DockPanel.Style>
           <Style>  
                <Setter 
                        Property="DockPanel.Background"
                             Value="Blue" /> 
               <Style.Triggers>                    
                   <DataTrigger 
                           Binding="{Binding Path=Test}"
                           Value="True">                        
                   <Setter 
                        Property="DockPanel.Background"
                             Value="Yellow" />                       
                    </DataTrigger>                </Style.Triggers>            </Style>        </DockPanel.Style>    </DockPanel></DockPanel>
Andrew Shepherd
Thanks! - It's always funny just that one little oversight that makes everything not work.
John
Thanks, I went through 10 SO threads and you had the answer I needed :).
Tomáš Kafka