更改默认按钮突出显示WPF

时间:2016-08-24 08:41:23

标签: c# wpf xaml button

我正在尝试删除Button上的默认背景,但是当用户用鼠标悬停在按钮上时,突出显示橙色的Button。这是我到目前为止所提出的;

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="FontSize" Value="20" />
        <Setter Property="Margin" Value="5" />
        <Setter Property="MaxHeight" Value="30" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="Cursor" Value="Hand" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Orange"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

然而,当用户将鼠标悬停在按钮上时,它会以默认的蓝色突出显示,而不是我想要的橙色。如何修改此按钮以使Button的文本逐渐以橙色突出显示?

1 个答案:

答案 0 :(得分:2)

您需要添加ControlTemplate.Trigger

<Style TargetType="{x:Type Button}">
 <Setter Property="FontSize" Value="20" />
 <Setter Property="Margin" Value="5" />
 <Setter Property="MaxHeight" Value="30" />
 <Setter Property="Width" Value="150" />
 <Setter Property="Background" Value="Transparent" />
 <Setter Property="Cursor" Value="Hand" />
 <Setter Property="Template">
    <Setter.Value>
       <ControlTemplate TargetType="{x:Type Button}">
          <Border Background="{TemplateBinding Background}" 
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
             <ContentPresenter HorizontalAlignment="Center" 
                               VerticalAlignment="Center"/>
          </Border>
           <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" 
                          Value="True">
                   <Setter Property="Background" 
                           Value="Orange"/>
                </Trigger>
        </ControlTemplate.Triggers>
       </ControlTemplate>
    </Setter.Value>
 </Setter>