按钮BorderBrush不会改变

问题描述

我的按钮模板的

BorderBrush颜色不会更改为Yellow。请帮忙。

<Style x:Key="someName" targettype="{x:Type Button}">
   <Setter Property="Background" Value="LightGreen"/>
   <Setter Property="BorderBrush" Value="Yellow"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate targettype="{x:Type Button}">
            <Border Background="{TemplateBinding Background}">
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
   <Style.Triggers>
      <Trigger Property="IsMouSEOver" Value="True">
         <Setter Property="Background" Value="Green"/>
      </Trigger>
   </Style.Triggers>
</Style>

解决方法

您必须通过BorderBrushTemplateBinding绑定到模板控件,以便它应用您样式中定义的值。此外,您必须以相同的方式设置至少添加一个BorderThickness,否则就没有边框。

<Style x:Key="someName" TargetType="{x:Type Button}">
   <Setter Property="Background" Value="LightGreen"/>
   <Setter Property="BorderBrush" Value="Yellow"/>
   <Setter Property="BorderThickness" Value="1"/>
   <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>
      </Setter.Value>
   </Setter>
   <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
         <Setter Property="Background" Value="Green"/>
      </Trigger>
   </Style.Triggers>
</Style>

作为一般说明,控件模板定义控件的所有部分和状态以及它们之间的过渡。您的控制模板定义了 Mouse Over 状态和正常状态。缺少状态可能会损害用户体验。您可以在documentation中找到按钮的所有部分和状态的列表。

建议extract the default control template or style首先使用Blend或Visual Studio进行控制,然后对其进行调整。我已经提取了默认样式并将其与您的样式合并。您只需要调整 Disabled Pressed 颜色。

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
   <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
   <Setter Property="Background" Value="LightGreen"/>
   <Setter Property="BorderBrush" Value="Yellow"/>
   <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" 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="Green"/>
                  <Setter Property="BorderBrush" TargetName="border" Value="Yellow"/>
               </Trigger>
               <Trigger Property="IsPressed" Value="true">
                  <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
                  <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
               </Trigger>
               <Trigger Property="IsEnabled" Value="false">
                  <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                  <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                  <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>