Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackgroundColor Items ComboBox WPF

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

like image 853
IrApp Avatar asked Jan 10 '23 02:01

IrApp


1 Answers

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

like image 140
default Avatar answered Jan 11 '23 22:01

default