28

<Separator />在我的表单中使用但不知道如何更改它的颜色。Border//都不存在ForegroundBackground请帮忙。

4

5 回答 5

72

您可以设置背景:

<Separator Background="Red"/>
于 2012-10-24T19:20:40.797 回答
22

嗯...我认为这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>
于 2012-08-24T19:47:03.937 回答
17

使用样式

    <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>

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

于 2010-10-22T07:54:55.303 回答
12

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

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

请注意,该BorderThickness属性也必须应用。

于 2015-03-17T23:23:45.573 回答
9

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

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

修改/形状更容易一些。

于 2012-08-30T10:48:09.773 回答