BackgroundColor Items ComboBox WPF

2019-06-24 01:32发布

I am doing a WPF and have a comboBox which has a list of the avaible ports on the computer. I want to change the color of these items.

My comboBox is these:

<ComboBox HorizontalAlignment="Left" Margin="445,0,0,0" VerticalAlignment="Top"     Width="120" Loaded="ComboBoxLoaded" SelectionChanged="ComboBoxSelectionChanged" Grid.Column="1" Background="#849096" Foreground="White"/>

And these is the method to loaded it:

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

I was trying a lot of things but nothing works. Someone knows how to do it? Thanks!!

1条回答
一夜七次
2楼-- · 2019-06-24 01:59

To change the background color of the individual items you can change the ItemContainerStyle, something like:

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

This will set the background color on the ComboBoxItems to Blue

查看更多
登录 后发表回答