在 WPF 中,触发器的顺序重要吗? [英] In WPF, does the order of Triggers matter?

查看:28
本文介绍了在 WPF 中,触发器的顺序重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xaml:

<DockPanel>
    <DockPanel.Resources>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Yellow"></Setter>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DockPanel.Resources>
    <Button Content="Cut" Height="30" Width="75"/>
</DockPanel>

发生的情况是,当我将鼠标悬停在按钮上时,前景变为黄色,然后当我按下按钮时,前景变为绿色.

What happens is that when I mouse over the button, the foreground changes to yellow and then when I press the button, the foreground changes to green.

现在,如果我颠倒 XAML 中触发器的顺序,当我将鼠标悬停在它上面时前景会变为黄色,但是当我按下按钮时,前景不会变为绿色.

Now, if I reverse the order of my triggers in the XAML, the foreground changes to yellow when I mouse over it, but when I press the button, the foreground does NOT change to green.

这是什么解释?一个触发器是否覆盖另一个触发器?

What is the explanation for this? Is one trigger overriding the other?

推荐答案

WPF 正在按声明的顺序处理您的触发器.在第二个示例中,前景非常短暂地变为绿色.但随后 IsMouseOver 触发器运行并将颜色设置回黄色.

WPF is processing your triggers in declared order. In the second example the foreground is ever so briefly changed to green. But then the IsMouseOver trigger runs and sets the color back to yellow.

IsMouseOver 在优先级方面与 IsPressed 没有关系.重要的是触发器在 XAML 中的声明顺序.

IsMouseOver has no relationship to IsPressed in terms of precedence. What's important is the declaration order in XAML of the triggers.

这篇关于在 WPF 中,触发器的顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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