¿Cómo cambiar el color de un WPF `<Separator/>`?
xaml colors (5)
Alternativamente, puedes elegir usar un elemento Rectangle:
<Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="2"/>
Es algo más fácil de modificar / dar forma.
Utilizo <Separator />
en mi formulario pero no sé cómo cambiar su color. Ninguno de Border
/ Foreground
/ Background
existe. Por favor ayuda
Hmm ... Creo que el Separator
es uno de los pocos elementos que no funcionará con un estilo simple. Según la documentación de MSDN, debe especificar el SeparatorStyleKey
.
Por ejemplo, para una ToolBar
haría esto:
<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>
Puede establecer el fondo:
<Separator Background="Red"/>
Puedes configurar el color del Separator
usando este código:
<Separator BorderBrush="Red" BorderThickness="1"/>
NOTA que la propiedad BorderThickness
debe aplicarse también.
Usar estilos
<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>
Un separador es solo un elemento de borde y ahora puede cambiar su apariencia de la forma que desee.