数据触发器未触发 [英] DataTrigger not firing

查看:44
本文介绍了数据触发器未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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>

现在 - 2 个复选框正确链接 - 检查一个将检查另一个 - 但数据触发器根本没有触发.

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

我做错了什么?

推荐答案

这里的问题是 属性值优先级.

您当前正在 DockPanel 上直接将背景设置为蓝色.此显式属性将覆盖触发器设置的任何值.

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>

这篇关于数据触发器未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆