BackgroundColor ComboBox WPF Elements

I am doing WPF and have a comboBox that has a list of available ports on the computer. I want to change the color of these elements.

My comboBox:

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

And this is the way to download 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 tried a lot of things, but nothing works. Does anyone know how to do this? Thank!!

+4
source share
1 answer

To change the background color of individual elements, you can change ItemContainerStylesomething like:

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

This will set the background color ComboBoxItemtoBlue

+7
source

All Articles