在代码隐藏中设置按钮的背景后,按钮的触发器 IsMouseOver 停止工作

Button's trigger IsMouseOver stops working after button's background is set in code-behind

我希望当光标悬停在按钮上时,按钮的背景会发生变化。
我设法以一种使用“IsMouseOver”触发器的样式来完成此操作,该触发器将按钮的背景设置为红色。

我还希望按钮在点击事件发生时在两种颜色之间改变背景。
我已经在 Window 的代码隐藏中做到了这一点,它在蓝色之间切换和绿色。

问题
只要我不单击按钮,触发器就会按预期工作。
当我单击该按钮时,背景会按预期更改为蓝色或绿色。
如果我随后将鼠标悬停在按钮上,则在鼠标悬停时背景不会设置为红色。

XAML-代码

<Window x:Class="Main.MainWindow">

    <Window.Resources>
        <Style x:Key="FooStyle" TargetType="Button">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Background" Value="Blue" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}" BorderBrush="Transparent">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <Button x:Name="btnFoo"
                Content="Foo"
                Width="100" 
                Click="Foo_Click" 
                Style="{StaticResource FooStyle}" />
    </StackPanel>

</Window>

Window 代码隐藏

private void Foo_Click(object sender, RoutedEventArgs e)
{
    btnFoo.Background = btnFoo.Background == Brushes.Blue ? Brushes.Lime : Brushes.Blue;
}

我怀疑触发器确实有效,但这里有其他问题。

可能是什么问题?
如何解决?

<Window.Resources>
            <Style x:Key="FooStyle" TargetType="Button">
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="12" />
                <Setter Property="Background" Value="Blue" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}" BorderBrush="Transparent">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
            <StackPanel Orientation="Vertical">
                <Button x:Name="btnFoo"
                    Content="Foo"
                    Width="100" 
                    Click="BtnFoo_Click" 
                    MouseEnter="BtnFoo_MouseEnter"
                    MouseLeave="BtnFoo_MouseLeave"
                    Style="{StaticResource FooStyle}" />
            </StackPanel>
    
        </Grid>

Code Behind

    Brush color { get; set; }
            private void BtnFoo_Click(object sender, RoutedEventArgs e)
            {
                color= color == Brushes.Lime ? Brushes.Blue : Brushes.Lime;
                btnFoo.Background = color;
            }
    
            private void BtnFoo_MouseEnter(object sender, MouseEventArgs e)
            {
              
                btnFoo.ClearValue(Button.BackgroundProperty);
            }
    
            private void BtnFoo_MouseLeave(object sender, MouseEventArgs e)
            {
                btnFoo.Background = color==null?btnFoo.Background:color;
            }

What could be the problem?

docs 中所述,本地值优先于样式设置器和触发器这一事实。

How to solve this?

例如将触发器移动到 ControlTemplate:

<Style x:Key="FooStyle" TargetType="Button">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="Background" Value="Blue" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="Transparent">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="Background" Value="Red" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>