【问题标题】:Change a button's background on mouseover [duplicate]在鼠标悬停时更改按钮的背景[重复]
【发布时间】:2019-02-05 19:49:24
【问题描述】:

当鼠标悬停在两个按钮上时,我必须更改它们的颜色,我已经搜索并遵循了许多教程但无法使其工作。

这是应用的样式:

<Window.Resources>
    <Style x:Key="btnStyleBase" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="FontFamily" Value="{StaticResource FontAwesome}"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Width" Value="42"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Button.Background" Value="Transparent"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="btnStyleClose" TargetType="{x:Type Button}" BasedOn="{StaticResource btnStyleBase}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Button.Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

这是实现样式的按钮:

<Button Name="btn1" Style="{StaticResource btnStyleBase}" Click="..." />
<Button Name="btn2" Style="{StaticResource btnStyleClose}" Click="..." />

“IsMouseOver”属性被触发,但即使它可以应用任何其他设置器,按钮的背景仍保持默认浅蓝色

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    默认情况下,WPF 控件有一个 ControlTemplate,它在操作系统后面定义。

    就像 Web 开发一样,每当您在不同版本的 Microsoft Windows(Win7、Win8、Win10、...)上运行应用程序时,您都会看到每个控件的不同显示

    如果你想摆脱这种变化,你必须为每个控件重写 ControlTemplate。

    对于 Button,您可以使用它(并根据需要更改它): [这个模板还有颜色变化的动画,你可以自定义]

     <Style TargetType="Button" x:Key="ButtonBaseStyle">
         <Setter Property="Padding" Value="12,6"/>
         <Setter Property="BorderThickness" Value="1"/>
     </Style>
    
     <Style TargetType="Button" x:Key="PrimaryButton" BasedOn="{StaticResource ButtonBaseStyle}">
         <Setter Property="BorderBrush" Value="#2e6da4"/>
         <Setter Property="Background" Value="#337ab7"/>
         <Setter Property="Foreground" Value="#fff"/>
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="Button">
                     <Border CornerRadius="4" Name="container" Cursor="Hand" Padding="{TemplateBinding Padding}" 
                             BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"
                             Background="{TemplateBinding Background}">
                         <ContentPresenter ContentSource="{TemplateBinding Content}" 
                             ContentTemplate="{TemplateBinding ContentTemplate}"
                             VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                         <VisualStateManager.VisualStateGroups>
                             <VisualStateGroup>
                                 <VisualState Name="Normal">
    
                                 </VisualState>
                                 <VisualState Name="MouseOver">
                                     <Storyboard>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#286090"></ColorAnimation>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#204d74"></ColorAnimation>
                                     </Storyboard>
                                 </VisualState>
                                 <VisualState Name="Pressed">
                                     <Storyboard>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#204d74"></ColorAnimation>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#122b40"></ColorAnimation>
                                     </Storyboard>
                                 </VisualState>
                                 <VisualState Name="Disabled">
                                     <Storyboard>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#337ab7"></ColorAnimation>
                                         <ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" 
                                                         Duration="0:0:0.02" To="#2e6da4"></ColorAnimation>
                                         <DoubleAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="Opacity"
                                                          Duration="0:0:0.02" To="0.8"></DoubleAnimation>
                                     </Storyboard>
                                 </VisualState>
                             </VisualStateGroup>
                         </VisualStateManager.VisualStateGroups>
                     </Border>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>
    

    有像 Bootstrap WPF 和 Material Design WPF 这样的开源解决方案,您可以阅读和更改它们。

    【讨论】:

    • 知道了!我也会搜索 bootstrap wpf
    【解决方案2】:

    我认为它正在寻找一个名为Button 的属性和一个名为Background 的子属性。把Button.Background改成Background,你的状态应该不错:

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent"/>
        </Trigger>
    </Style.Triggers>
    

    【讨论】:

    • 即使改变它,你也会在 Win7 的 MouseOver 上看到light blue。
    • 我在 Windows 10 上并且没有在 Win7 上进行快速测试的方法,但我不记得使用这种方法遇到了特定于操作系统的问题......因为它是 10 秒修复,您可能希望在重写整个模板之前至少测试这种方法。
    • 亲爱的@Elemental_Pete 我已经为我的项目测试了很多次。甚至在更改操作系统版本时,控件的边距和其他控件中的填充也发生了变化。 Microsoft 没有花更多时间为 WPF 创建主题。
    • 我不是质疑默认 XP 按钮看起来与 Windows 7 按钮不同,我只是质疑您无法在 Windows 7 中自定义控件属性的前提。您应该能够更改其中任何一个的颜色,即使默认情况下其他属性看起来有点不同。这些差异提供了符合操作系统的“本机”外观,而如果您需要您的按钮在所有平台上看起来都相同,则自定义按钮模板会更好(即使它们看起来与所有其他按钮不同)你的操作系统)
    • 其实@Fehu的主要问题是识别Style.Triggers中的所有条件。如果您知道按钮默认值中的所有触发器,则可以对其进行自定义。你是对的,但我更愿意防止未来的错误(最好说:Chages)。所以在这些情况下,我正在寻找一个 ControlTemplate。
    猜你喜欢
    • 2017-07-28
    • 2017-12-15
    • 2014-10-16
    • 2015-10-02
    • 1970-01-01
    • 2018-07-07
    • 2018-01-15
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多