更改默认按钮,突出显示WPF

本文关键字:显示 WPF 默认按钮 | 更新日期: 2023-09-27 18:03:55

我试图删除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>

但是,当用户将鼠标放在按钮上时,它仍然以默认的蓝色高亮显示,而不是我想要的橙色。我如何修改这一点,使按钮的文本逐渐突出显示为橙色?

更改默认按钮,突出显示WPF

您需要添加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>