如何在WPF中更改Button MouseOver的背景?

Sep*_*adi 82 wpf triggers background mouseover button

我在这个XAML页面上有一个按钮:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
    Width="50" Height="50" HorizontalContentAlignment="Left" 
    BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Background" Value="Green"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)

但是当我将鼠标放在我的按钮上时,按钮的背景会变为默认的窗口灰色背景.
有什么问题?

这是鼠标悬停之前和之后的按钮图片:
之前:
之前
后:
后

Ric*_*d E 148

要删除默认MouseOver行为,Button您需要修改ControlTemplate.将您的Style定义更改为以下内容应该可以解决问题:

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Green"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

编辑:它已经晚了几年,但你实际上可以在那里的边框内设置边框画笔.Idk如果有人指出但它似乎不是......

  • @CF的原因是[标准按钮样式](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709909.aspx)在`ControlTemplate`中有触发器,所以它们覆盖OP的'Style`触发器. (4认同)

Pet*_*iho 12

到目前为止,所有答案都包括用其他方式完全替换默认按钮行为。但是,恕我直言,了解通过编辑XAML元素的现有默认模板可以更改您关心的部分是有用且重要的。

如果要处理WPF按钮上的悬停效果,则WPF Button元素中外观的更改是由引起TriggerButton,这是基于的默认样式的,该样式基于IsMouseOver属性并设置顶级元素的BackgroundBorderBrush属性Border在控制模板中。该Button元素的背景是下面Border元素的背景,因此更改Button.Background属性不会阻止看到悬停效果。

通过一些努力,您可以使用自己的设置器来覆盖此行为,但是由于您需要影响的元素位于模板中,并且无法在您自己的XAML中直接访问,因此该方法将很困难并且IMHO过于复杂。

另一种选择是利用图形作为ContentButton,而不是Background。如果您需要图形上的其他内容,可以将它们与Grid作为内容中的顶级对象组合。

但是,如果您只是想完全禁用悬停效果(而不仅仅是隐藏它),则可以使用Visual Studio XAML设计器:

  1. 编辑XAML时,选择“设计”选项卡。
  2. “设计”选项卡中,找到要禁用效果的按钮。
  3. 右键单击该按钮,然后选择“编辑模板/编辑副本...”。在提示中选择要在其中放置新模板资源的位置。这似乎无济于事,但实际上,Designer会在您指定的位置添加新资源,并更改了按钮元素以引用将这些资源用作按钮模板的样式。
  4. 现在,您可以编辑该样式。最简单的方法是删除或注释掉元素(例如Ctrl+ EC<Trigger Property="IsMouseOver" Value="true">...</Trigger>。当然,您可以在那时更改所需的模板。

完成后,按钮样式将如下所示:

<p:Style x:Key="FocusVisual">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate>
        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</p:Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
<p:Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
  <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
  <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
  <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="HorizontalContentAlignment" Value="Center"/>
  <Setter Property="VerticalContentAlignment" Value="Center"/>
  <Setter Property="Padding" Value="1"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
          <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsDefaulted" Value="true">
            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
          </Trigger>
          <!--<Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
          </Trigger>-->
          <Trigger Property="IsPressed" Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</p:Style>
Run Code Online (Sandbox Code Playgroud)

(注意:您可以p:在实际代码中省略XML名称空间的限定条件……我在这里提供它们仅是因为Stack Overflow XML代码格式化程序被<Style/>那些没有使用XML名称空间完整名称的元素所混淆。)

如果要对其他按钮应用相同的样式,只需右键单击它们,然后选择“编辑模板/应用资源”,然后选择刚为第一个按钮添加的样式。您甚至可以使用将默认样式应用于XAML中的元素的常规技术,将该样式设置为所有按钮的默认样式。

  • 非常感谢。这是这里唯一可以忍受的答案 (2认同)

Con*_*ngo 10

这对我很有用.

按钮样式

<Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="DarkGoldenrod"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid Background="Transparent">
                        <ContentPresenter></ContentPresenter>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

按键

<Button Style="{StaticResource TransparentStyle}" VerticalAlignment="Top" HorizontalAlignment="Right" Width="25" Height="25"
        Command="{Binding CloseWindow}">
    <Button.Content >
        <Grid Margin="0 0 0 0">
            <Path Data="M0,7 L10,17 M0,17 L10,7" Stroke="Blue" StrokeThickness="2" HorizontalAlignment="Center" Stretch="None" />
        </Grid>
    </Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)

笔记

  • 该按钮显示一个小蓝色十字架,非常类似于用于关闭窗户的十字架.
  • 通过将网格的背景设置为"透明",它会添加一个hittest,这意味着如果鼠标位于按钮上方的任何位置,那么它将起作用.省略此标记,只有当鼠标位于图标中的某个矢量线上时,该按钮才会亮起(这不是很有用).


小智 6

只想从我一直在使用的 ResourceDictionary 分享我的按钮样式。您可以在样式触发器处自由更改 onHover 背景。“ ColorAnimation To = *您想要的BG(即#FFCEF7A0)”。在 mouseOver 状态后,按钮 BG 也会自动恢复到其原始 BG。您甚至可以设置过渡的速度。

资源字典

<Style x:Key="Flat_Button" TargetType="{x:Type Button}">
    <Setter Property="Width" Value="100"/>
    <Setter Property="Height" Value="50"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="FontFamily" Value="Arial Narrow"/>
    <Setter Property="FontSize" Value="12px"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Foreground">
        <Setter.Value>
            <SolidColorBrush Opacity="1" Color="White"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Background" >
        <Setter.Value>
            <SolidColorBrush Opacity="1" Color="#28C2FF" />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Border x:Name="border"
                         SnapsToDevicePixels="True"
                         BorderThickness="1"
                         Padding="4,2"
                         BorderBrush="Gray"
                         CornerRadius="3"
                         Background="{TemplateBinding Background}">
                    <Grid>
                        <ContentPresenter 
                        Margin="2"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        RecognizesAccessKey="True" />

                    </Grid>
                </Border>

            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation To="#D2F898"
                                        Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" 
                                        FillBehavior="HoldEnd" Duration="0:0:0.25" AutoReverse="False" RepeatBehavior="1x"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>

            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                                            Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" 
                                            FillBehavior="HoldEnd" Duration="0:0:0.25" AutoReverse="False" RepeatBehavior="1x"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>

        </Trigger>


    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

您所要做的就是调用样式。

示例实现

<Button Style="{StaticResource Flat_Button}" Height="Auto"Width="Auto">  
     <StackPanel>
     <TextBlock Text="SAVE" FontFamily="Arial" FontSize="10.667"/>
     </StackPanel>
</Button>
Run Code Online (Sandbox Code Playgroud)