简体   繁体   中英

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

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 ComboBoxItem s to Blue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM