悬停圆角按钮使用ResourceDictionary更改其背景颜色

时间:2020-02-02 18:05:00

标签: c# wpf xaml

我创建了一个ResourceDictionary样式表,该样式表的样式名为btn_default。 我打算在程序的所有按钮上使用它。我的问题是,当我将鼠标悬停在按钮上时,背景颜色不会改变。字体颜色改变。

button_default

enter image description here

button_default:悬停

enter image description here

我尝试在代码中更改“ Setter Property =“ Background” Value =“#b5b5b5”“,但是我想这不会影响Border-tag,但会影响Style-tag?

ResourceDictionary

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MSDNSample">

    <!-- Btn default -->
    <Style x:Key="btn_default" TargetType="{x:Type Button}">
        <Setter Property="FontFamily" Value="Calibri"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Foreground" Value="#d9d9d9" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5" BorderThickness="1" Padding="6,4,6,4" Background="#6c6c6c" BorderBrush="#393939">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#b5b5b5"/>
                            <Setter Property="Foreground" Value="#000000" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>
    <!-- //Btn default -->
</ResourceDictionary>

主要

<Button x:Name="buttonExplorer" Content="Explorer" Style="{StaticResource btn_default}" Margin="0,0,6,0" />
<Button x:Name="buttonProcess" Content="Process" Style="{StaticResource btn_default}" Margin="0,0,6,0" />

1 个答案:

答案 0 :(得分:2)

您应使用Background扩展名声明Border的{​​{1}}属性,并在TemplateBinding设置器中设置Background的值,否则它将永远不会更新

Style