如何更改 WPF `` 的颜色?

发布于 2024-09-28 10:35:12 字数 142 浏览 0 评论 0原文

我在表单中使用 但不知道如何更改其颜色。 Border /Foreground/Background都不存在。请帮忙。

I use <Separator /> in my form but don't know how to change its color. None of Border /Foreground/Background does exist. Plese help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩饰不了的爱 2024-10-05 10:35:12

您可以设置背景:

<Separator Background="Red"/>

You can set the Background:

<Separator Background="Red"/>
彩虹直至黑白 2024-10-05 10:35:12

嗯...我认为 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>

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>
云巢 2024-10-05 10:35:12

使用样式

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

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

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?

記柔刀 2024-10-05 10:35:12

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

请注意 BorderThickness< /code> 属性也必须应用。

you can set the Separator's color using this code:

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

NOTE that the BorderThickness property must be applied too.

酒几许 2024-10-05 10:35:12

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

修改/形状更容易一些。

Alternatively you could choose to use a Rectangle element:

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

It's somewhat easier to modify/shape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文