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

查看:214
本文介绍了在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中的触发器顺序,则当我将其悬停在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天全站免登陆