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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With