【问题标题】:How to change the color of the a WPF `<Separator />`?如何更改 WPF `<Separator />` 的颜色?
【发布时间】:2023-03-09 09:15:01
【问题描述】:

我在表单中使用&lt;Separator /&gt;,但不知道如何更改其颜色。 Border /Foreground/Background 都不存在。请帮忙。

【问题讨论】:

    标签: wpf xaml colors separator


    【解决方案1】:

    嗯...我认为Separator 是少数无法使用简单样式的元素之一。根据 MSDN 文档,您需要指定SeparatorStyleKey

    例如,对于ToolBar,您可以这样做:

    <Style x:Key="{x:Static ToolBar.SeparatorStyleKey}" 
        TargetType="{x:Type Separator}">
        <Setter Property="Background" 
            Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
        <Setter Property="Margin" Value="0,2,0,2"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Border 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        Height="1" 
                        SnapsToDevicePixels="true"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • +1 是唯一适用于工具栏分隔符的答案。其他答案均无效。
    • 键名很重要。
    • 顺便说一句,这里有一篇文章解释了原因,还提到了如何设置其他类型的分隔符,即 MenuItem 的分隔符:devlicio.us/blogs/christopher_bennage/archive/2008/06/19/…
    • 这也适用于菜单项:x:Key="{x:Static MenuItem.SeparatorStyleKey}
    【解决方案2】:

    您可以使用以下代码设置Separator 的颜色:

    &lt;Separator BorderBrush="Red" BorderThickness="1"/&gt;

    注意BorderThickness 属性也必须应用。

    【讨论】:

    • 我可以通过设置 BorderThickness="1 0 0 0" 来实现它。否则,分隔符厚两个像素。
    • 方向很重要...水平方向,你想要BorderThickness="0 1 0 0"。此外,UseLayoutRounding 可能是必要的以避免外观模糊。
    【解决方案3】:

    你可以设置背景:

    <Separator Background="Red"/>
    

    【讨论】:

      【解决方案4】:

      您也可以选择使用 Rectangle 元素:

      &lt;Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="2"/&gt;

      修改/形状稍微容易一些。

      【讨论】:

      • 有没有办法使矩形在上下文菜单中不可选择?分隔符的好处是您无法突出显示它,它不是真正的 MenuItem 子项
      【解决方案5】:

      使用样式

          <Style x:Key="MySeparatorStyle" TargetType="{x:Type Separator}">
              <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
              <Setter Property="Margin" Value="0,2,0,2"/>
              <Setter Property="Focusable" Value="false"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type Separator}">
                          <Border 
                              BorderBrush="{TemplateBinding BorderBrush}" 
                              BorderThickness="{TemplateBinding BorderThickness}" 
                              Background="{TemplateBinding Background}" 
                              Height="1" 
                              SnapsToDevicePixels="true"/>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      分隔符只是一个边框元素,现在您可以随意更改其外观?

      【讨论】:

      • 它不适用于工具栏分隔符,请查看下面@code4life 的答案。
      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      相关资源
      最近更新 更多