微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改IsMouseOver上ResourceResourceDirectory中按钮的样式

如何解决更改IsMouseOver上ResourceResourceDirectory中按钮的样式

我的按钮样式类似于ResourceDirectory

<Button x:Name="btnPO_Manager" Height="23" Content="PO Manager" Click="Menu_Button" Style="{StaticResource button_Menu}" />

这是我的ResourceDirectory Style

<Color x:Key="color_BaseFont">Black</Color>
<Color x:Key="color_MajorFont">#d8b243</Color>
<Color x:Key="color_MinorFont">#a01e21</Color>
<Color x:Key="color_MajorBackground">Black</Color>
<Color x:Key="color_MinorBackground">#FF272727</Color>


<SolidColorBrush x:Key="brush_BaseFont" Color="{StaticResource color_BaseFont}"/>
<SolidColorBrush x:Key="brush_MajorFont" Color="{StaticResource color_MajorFont}"/>
<SolidColorBrush x:Key="brush_MinorFont" Color="{StaticResource color_MinorFont}"/>
<SolidColorBrush x:Key="brush_MajorBackground" Color="{StaticResource color_MajorBackground}"/>
<SolidColorBrush x:Key="brush_MinorBackground" Color="{StaticResource color_MinorBackground}"/>


<Style BasedOn="{StaticResource {x:Type Button}}"
       targettype="Button"
       x:Key="button_Menu">
   <Setter Property="FontSize" Value="10"/>
   <Setter Property="Foreground" Value="{StaticResource brush_MajorFont}"/>
   <Setter Property="Background" Value="{StaticResource brush_MinorBackground}"/>

   <Style.Triggers>
      <Trigger Property="IsMouSEOver" Value="True">
         <Setter Property="Foreground" Value="{StaticResource brush_MinorFont}"/>
         <Setter Property="Background" Value="{StaticResource brush_MajorBackground}"/>
      </Trigger>
   </Style.Triggers>

</Style>

除了触发器background属性,这一切都很好。鼠标悬停时前景发生变化,但背景保持认的蓝色/灰色。

我尝试将认颜色移至IsMouSEOver = False,但结果相同。除鼠标悬停背景外,其他所有功能均正常工作。

<Style.Triggers>
   <Trigger Property="IsMouSEOver" Value="True">
      <Setter Property="Foreground" Value="{StaticResource brush_MinorFont}"/>
      <Setter Property="Background" Value="{StaticResource brush_MajorBackground}"/>
   </Trigger>
   <Trigger Property="IsMouSEOver" Value="False">
      <Setter Property="Foreground" Value="{StaticResource brush_MajorFont}"/>
      <Setter Property="Background" Value="{StaticResource brush_MinorBackground}"/>
   </Trigger>
</Style.Triggers>

为什么这样做,如何解决?任何帮助将不胜感激。

解决方法

您的按钮样式没有显示正确的背景颜色,因为它没有绑定到控件模板的 Mouse Over 状态下父按钮的Background属性,它静态地使用参考颜色。

要更改此设置,必须更改控制模板。请注意,控制模板很复杂。它们需要特定的部分和状态才能正常工作,您可以in the documentation找到它们。

最简单的方法是extract the default control template使用Blend或Visual Studio并使其适应您的需求。以下是根据您的自定义颜色调整的默认模板。

<Style x:Key="button_Menu_FocusVisual">
   <Setter Property="Control.Template">
      <Setter.Value>
         <ControlTemplate>
            <Rectangle Margin="2" StrokeDashArray="1 2" SnapsToDevicePixels="true" StrokeThickness="1" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<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"/>
<Style x:Key="button_Menu" TargetType="{x:Type Button}">
   <Setter Property="FocusVisualStyle" Value="{StaticResource button_Menu_FocusVisual}"/>
   <Setter Property="Background" Value="{StaticResource brush_MinorBackground}"/>
   <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
   <Setter Property="Foreground" Value="{StaticResource brush_MajorFont}"/>
   <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" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 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 brush_MajorBackground}"/>
                  <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                  <Setter Property="TextElement.Foreground" TargetName="border" Value="{StaticResource brush_MinorFont}"/>
               </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>
</Style>

您也可以更改其他默认画笔,或用您自己的颜色替换它们。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。