背景颜色 项目 组合框 WPF

4
我正在进行一个WPF项目,其中有一个下拉框,列出了计算机上可用的端口。我想要改变这些选项的颜色。
我的下拉框如下:
<ComboBox HorizontalAlignment="Left" Margin="445,0,0,0" VerticalAlignment="Top"     Width="120" Loaded="ComboBoxLoaded" SelectionChanged="ComboBoxSelectionChanged" Grid.Column="1" Background="#849096" Foreground="White"/>

这是加载它的方法:
private void ComboBoxLoaded(object sender, RoutedEventArgs e)
    {
        string [] portsList = PrintPorts();

        // get the ComboBox reference
        var comboBox = sender as ComboBox;

        // assign the ItemsSource to the List
        comboBox.ItemsSource = portsList;

        // make the first item selected
        comboBox.SelectedIndex = 0;
    }

我尝试了很多方法,但都没有起作用。有人知道怎么做吗?谢谢!!
1个回答

10

要更改个别项目的背景颜色,您可以更改 ItemContainerStyle ,例如:

要更改个别项目的背景颜色,您可以更改 ItemContainerStyle ,例如:

    <ComboBox>
        ...
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="Background" Value="Blue" />
            </Style>
        </ComboBox.ItemContainerStyle>
        ...
    </ComboBox>

这将把ComboBoxItem的背景颜色设置为Blue


使用这段代码,每个项目周围都会留下一点边框...我该如何更改代码,使得整个下拉面板都是蓝色的? - Martina
1
我相信这是一个有效的新问题。您应将其作为新问题发布,并添加屏幕截图以展示它的样子和期望结果。请注意,不同的操作系统有不同的风格,因此还要提及目标操作系统。 - default
https://dev59.com/Apjga4cB1Zd3GeqPKniO - Martina

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接