简体   繁体   English

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

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

I want the background of a button to change when the cursor hovers over the button.当 cursor 悬停在按钮上时,我希望更改按钮的背景。
I have managed to do this in a style which use the "IsMouseOver" trigger which sets the button's background to red.我设法以一种使用“IsMouseOver”触发器的样式来做到这一点,该触发器将按钮的背景设置为红色。

I also want the button to alter it's background between two colors when the click event occurs.当点击事件发生时,我还希望按钮在两个 colors 之间改变它的背景。
I have managed to do this in the code-behind of the Window which switches between blue and green.我设法在 Window 的代码隐藏中做到了这一点,它在蓝色和绿色之间切换。

The problem问题
The trigger works as expected as long as I don't click the button.只要我不单击按钮,触发器就会按预期工作。
When I click the button, the background is changed to either blue or green as expected.当我单击按钮时,背景会按预期更改为蓝色或绿色。
If I then afterwards hover the button, the background is not set to red while hovering with the cursor.如果我然后在 hover 按钮之后,使用 cursor 悬停时,背景不会设置为红色。

XAML-code 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 code-behind Window 代码隐藏

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

I suspect the trigger actually works but it something else here which is the problem.我怀疑触发器确实有效,但问题在于这里的其他东西。

What could be the problem?可能是什么问题呢?
How to solve this?如何解决这个问题?

What could be the problem?可能是什么问题呢?

The fact that local values take precedence over style setters and triggers as explained in the docs .文档中所述,本地值优先于样式设置器和触发器的事实。

How to solve this?如何解决这个问题?

For example by moving the trigger to the ControlTemplate :例如,通过将触发器移动到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>

在此处输入图像描述

<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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM