WPF 触发器未按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17758904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:15:00  来源:igfitidea点击:

WPF Trigger Not Working As Intended

wpfbuttontriggersstylesismouseover

提问by Clement Hoang

I want a red button that turns black when the mouse hovers over it.

我想要一个红色按钮,当鼠标悬停在它上面时会变黑。

    <Button Content="Hover me" Grid.Column="3" Grid.Row="3">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Red"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Black"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

However, my problem is that when I hover over the button, it turns into the default Windows style with a gradient gray appearance.

但是,我的问题是,当我将鼠标悬停在按钮上时,它会变成具有渐变灰色外观的默认 Windows 样式。

回答by Nitesh

Try it

尝试一下

<Window.Resources>
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter x:Name="PART_Content"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          TextElement.Foreground="{TemplateBinding Foreground}"></ContentPresenter>                        </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Black"/>
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

and apply the custom Style as following

并应用自定义样式如下

<Button Content="Hover me" Style="{StaticResource MyButtonStyle}" Height="30" Width="100"/>

The reason is the default Aero style of a Button. It has a chrome defined in ControlTemplate, which has it own behavior on various mouse events. So That over write your trigger call.

原因是按钮的默认 Aero 样式。它在 ControlTemplate 中定义了一个镶边,它在各种鼠标事件上都有自己的行为。所以那覆盖了你的触发器调用。

So you must override default ControlTemplate of Button to achieve your desired result.

因此,您必须覆盖 Button 的默认 ControlTemplate 才能获得所需的结果。