如何更改 WPF `<Separator />` 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3994596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the color of the a WPF `<Separator />`?
提问by Nam G VU
I use <Separator />
in my form but don't know how to change its color. None of Border
/Foreground
/Background
does exist. Plese help.
我<Separator />
以我的形式使用,但不知道如何更改其颜色。无的Border
/ Foreground
/Background
的确存在。请帮忙。
采纳答案by rudigrobler
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?
分隔符只是一个边框元素,现在您可以随心所欲地更改其外观吗?
回答by throop77
You can set the Background:
您可以设置背景:
<Separator Background="Red"/>
回答by code4life
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
.
嗯...我认为这Separator
是少数几个不能使用简单样式的元素之一。根据 MSDN 文档,您需要指定SeparatorStyleKey
.
For instance for a ToolBar
you would do this:
例如对于 aToolBar
你会这样做:
<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>
回答by sadovecki
you can set the Separator
's color using this code:
您可以Separator
使用以下代码设置的颜色:
<Separator BorderBrush="Red" BorderThickness="1"/>
<Separator BorderBrush="Red" BorderThickness="1"/>
NOTE that the BorderThickness
property must be applied too.
请注意,该BorderThickness
属性也必须被应用。
回答by Deruijter
Alternatively you could choose to use a Rectangle element:
或者,您可以选择使用 Rectangle 元素:
<Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="2"/>
<Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="2"/>
It's somewhat easier to modify/shape.
修改/塑造更容易一些。