How to change the color of the a WPF `<Separator />`? How to change the color of the a WPF `<Separator />`? wpf wpf

How to change the color of the a WPF `<Separator />`?


You can set the Background:

<Separator Background="Red"/>


Hmm... I think the Separator is one of the few elements that will not work using a simple style. Based on the MSDN documentation, you need to specify the SeparatorStyleKey.

For instance for a ToolBar you would do this:

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


Use styles

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

A seperator is just a border element and now you can change its appearance any way you like?