按钮颜色WPF

时间:2015-10-15 14:48:18

标签: c# wpf button

我正试图在鼠标悬停时更改我的按钮颜色,但它不起作用(按钮仍为蓝色),我发现的所有示例都像我正在做的那样:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#424242"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#8BC34A"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您应该像这样使用ControlTemplate

<Button.Style>
     <Style TargetType="{x:Type Button}">
          <Setter Property="Background" Value="#424242"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
                       <Border Background="{TemplateBinding Background}">
                          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                      </Border>
                  </ControlTemplate>
              </Setter.Value>
         </Setter>
         <Style.Triggers>
             <Trigger Property="IsMouseOver" Value="True">
                 <Setter Property="Background" Value="#8BC34A"/>
             </Trigger>
           </Style.Triggers>
     </Style>
</Button.Style>