更改 WPF 数据绑定组合框中分隔符的背景颜色

发布于 2024-10-03 20:01:54 字数 440 浏览 1 评论 0原文

我有一个绑定到数据集的组合框,然后在数据中遇到“-”时使用数据触发器插入分隔符(示例 在此问题中)

菜单的背景具有自定义颜色,通过使用资源字典设置。本例中的颜色是 #FFF8F4C5

如果我将分隔符添加到非数据绑定简单组合框,它会正确显示。但是,当使用数据触发器添加它时,它看起来不像菜单的其余部分,如下所示(它有白色背景)。

分隔符上的白色背景

如果我设置分隔符的背景,它实际上会将较暗的线条更改为任何颜色。我似乎找不到如何更改白色区域以匹配与菜单相同的颜色。

I have a combobox that is bound to a dataset that then uses a datatrigger to insert a separator when it encounters a '-' in the data (example in this question).

The background of the menu has custom color, set by using a resource dictionary. The color in this case is #FFF8F4C5

If I add a separator to a non databound simple combo box, it appears correctly. But when adding it using the datatrigger, it does not look like the rest of the menu, as you can see below (it has a white background).

white background on separator

If I set the background of the separator, it actually changes the darker line to whatever color. I can't seem to find how to change the white area to match the same color as the menu.

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

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

发布评论

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

评论(2

嘦怹 2024-10-10 20:01:54

在 ControlTemplate 中,将分隔符包含在边框中,并将背景绑定到父 ComboBoxItem 的背景。像这样的东西:

<ControlTemplate TargetType="{x:Type ComboBoxItem}">
    <Border Background="{TemplateBinding Background}">
        <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
    </Border>
</ControlTemplate>

In the ControlTemplate, enclose the Separator in a Border with Background bound to to the parent ComboBoxItem's Background. Something like this:

<ControlTemplate TargetType="{x:Type ComboBoxItem}">
    <Border Background="{TemplateBinding Background}">
        <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
    </Border>
</ControlTemplate>
婴鹅 2024-10-10 20:01:54

使用分隔符样式:

<Style x:Key="SeparatorStyle1" 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 Height="1" SnapsToDevicePixels="true" 
              Background="#FFCCD480" BorderBrush="#FF633A3A" BorderThickness="0,0,0,1"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并像这样使用

<ComboBox Background="#FFD2D2B5">
  <ListBoxItem Content="item1"/>
  <ListBoxItem Content="item2"/>
  <Separator Style="{DynamicResource SeparatorStyle1}"/>
  <ListBoxItem Content="item3"/>

它应该可以做到

use a separator style:

<Style x:Key="SeparatorStyle1" 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 Height="1" SnapsToDevicePixels="true" 
              Background="#FFCCD480" BorderBrush="#FF633A3A" BorderThickness="0,0,0,1"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and use it like this

<ComboBox Background="#FFD2D2B5">
  <ListBoxItem Content="item1"/>
  <ListBoxItem Content="item2"/>
  <Separator Style="{DynamicResource SeparatorStyle1}"/>
  <ListBoxItem Content="item3"/>

That should do it

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