开发者

DataTrigger not firing

开发者 https://www.devze.com 2022-12-09 10:20 出处:网络
I have the following xaml: <DockPanel> <DockPanel> <CheckBox IsChecked=\"{Bindi开发者_如何转开发ng Path=Test}\" />

I have the following xaml:

    <DockPanel>
    <DockPanel>
        <CheckBox IsChecked="{Bindi开发者_如何转开发ng 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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号