简体   繁体   English

按钮上的IsMouseOver属性不起作用

[英]IsMouseOver Property on Button not working

I have this button and wanted to change the design if I hover over it with the mouse. 我有这个按钮,如果我用鼠标悬停在它上面,我想改变设计。 It's not working and I'm not getting an error. 它没有用,我没有收到错误。 What am I doing wrong? 我究竟做错了什么? (I'm really new to WPF) (我是WPF的新手)

    <Button MaxWidth="180"
            Margin="5"
            DockPanel.Dock="Top"
            Padding="5"
            FontSize="12"
            Foreground="#1261AC"
            FontWeight="SemiBold"
            BorderBrush="Transparent">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border CornerRadius="5" Background="LightGray" BorderThickness="1" Padding="5">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="#157ec4"/>
                        <Setter Property="Background" Value="#000000"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

The button itself is working but it is not chaning the color of the background or font. 按钮本身正在工作,但它不会处理背景或字体的颜色。 (The colors in my example are just for testing) (我的示例中的颜色仅用于测试)

The problem with your code is that you directly define the border-background to be gray. 您的代码的问题是您直接将border-background定义为灰色。 Now you change the control background using a trigger. 现在,您可以使用触发器更改控件背景。 However, the background that is set by the trigger is not yet related to the border background in your example. 但是,触发器设置的背景与示例中的边框背景尚未关联。 I added a template binding that fixes this issue to you. 我添加了一个模板绑定,可以解决这个问题。 Now the border in your template will always have the Background defined in your style, set by triggers or directly set in XAML. 现在,模板中的边框将始终在您的样式中定义背景,由触发器设置或直接在XAML中设置。

PLEASE NOTE: If you set the color in XAML by using <Button Background="Pink"/> this will overwrite the style and trigger attributes. 请注意:如果使用<Button Background="Pink"/>在XAML中设置颜色,则会覆盖样式和触发器属性。

try this piece of art: 试试这件艺术品:

在此输入图像描述

<Button Content="Hello there!"
    MaxWidth="180"
    Margin="5"
    DockPanel.Dock="Top"
    Padding="5"
    FontSize="12"
    Foreground="#1261AC"
    FontWeight="SemiBold"
    BorderBrush="Transparent">
<Button.Style>
    <Style TargetType="Button">
        <Setter Property="Background" Value="HotPink"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" Padding="5">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="Background" Value="Lime" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

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

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