In WPF, does the order of Triggers matter?

WpfTriggers

Wpf Problem Overview


I have the following 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.

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 Solutions


Solution 1 - Wpf

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 has no relationship to IsPressed in terms of precedence. What's important is the declaration order in XAML of the triggers.

Solution 2 - Wpf

In short: triggers are processed in order.

Later triggers override earlier triggers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScottGView Question on Stackoverflow
Solution 1 - WpfJaredParView Answer on Stackoverflow
Solution 2 - WpfDrew NoakesView Answer on Stackoverflow