c# - BackgroundColor Items ComboBox WPF

标签 c# .net visual-studio xaml

我正在做一个 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;
    }

我尝试了很多东西,但没有任何效果。有人知道怎么做吗?谢谢!!

最佳答案

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

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

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

关于c# - BackgroundColor Items ComboBox WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27963236/

相关文章:

c# - 无法使用 Datetime.ParseExact 转换日期时间字符串

javascript - Signalr:JavaScript 客户端无法在 IE 上重新连接(parseerror 异常)

visual-studio - TFS离线时无法打开解决方法

c# - 通过ASP.NET页面进行音频对话!

c# - 以编程方式使用附加字段扩展 Lucene 文档

c# - Visual Studio 和 Eclipse 哪个更好

c# - 如何在 DataGridView 中获取选定的数据行?

c# - 为 64 位操作系统包装 32 位 dll 以与 regsvr32.exe 一起使用

c# - 在 NopCommerce 中创建自定义任务

c++ - C++ 中的数据类型