在WPF中为按钮设置鼠标悬停颜色

时间:2018-06-11 08:58:54

标签: c# wpf wpf-controls

我在WPF应用程序中使用以下样式的按钮。在这里,如何设置按钮的鼠标悬停颜色?

 <Style TargetType="{x:Type Button}" x:Key="ButtonStyle2">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Background" Value="#FF45414D" />
    <Setter Property="Height" Value="35" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="6,6,6,6" Background="#FF45414D" >
                    <ContentPresenter x:Name="contentPresenter" 
                                      ContentTemplate="{TemplateBinding ContentTemplate}" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                      Margin="{TemplateBinding Padding}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后我将代码更改为以下内容,将鼠标添加到属性上。 Click here 但它仍然不起作用。

<Style TargetType="{x:Type Button}" x:Key="ButtonStyle2">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Background" Value="#FF45414D" />
    <Setter Property="Height" Value="35" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="6,6,6,6" Background="#FF45414D" >
                    <ContentPresenter x:Name="contentPresenter" 
                                      ContentTemplate="{TemplateBinding ContentTemplate}" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                      Margin="{TemplateBinding Padding}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      />

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:0)

如何为边框编写触发器?

<Style TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="16" />
<Setter Property="Background" Value="Red" />
<Setter Property="Height" Value="35" />
<Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border >
                    <ContentPresenter x:Name="contentPresenter" 
                      ContentTemplate="{TemplateBinding ContentTemplate}" 
                      Content="{TemplateBinding Content}" 
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                      Margin="{TemplateBinding Padding}" 
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      />
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="#FF45414D"/>
                            <Setter Property="CornerRadius" Value="6,6,6,6"/>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="Red"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您可以在边框标记中为背景更改编写触发器。