如何更改WPF"<Separator />"的颜色?

Nam*_* VU 24 wpf xaml colors separator

我用<Separator />我的形式,但不知道如何改变它的颜色.没有Border/ Foreground/ Background确实存在.请帮助.

thr*_*p77 64

您可以设置背景:

<Separator Background="Red"/>
Run Code Online (Sandbox Code Playgroud)


cod*_*ife 21

嗯......我认为这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>
Run Code Online (Sandbox Code Playgroud)

  • 这也适用于菜单项:x:Key ="{x:Static MenuItem.SeparatorStyleKey} (2认同)

rud*_*ler 13

使用样式

    <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>
Run Code Online (Sandbox Code Playgroud)

分隔符只是一个边框元素,现在您可以按照自己喜欢的方式更改其外观?


小智 10

你可以Separator使用这段代码设置颜色:

<Separator BorderBrush="Red" BorderThickness="1"/>

请注意,该BorderThickness物业也必须适用.

  • 我可以通过设置 `BorderThickness="1 0 0 0"` 来实现它。否则,分隔符的厚度为两个像素。 (3认同)

Der*_*ter 8

或者,您可以选择使用Rectangle元素:

<Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="2"/>

修改/形状更容易一些.