本文介绍了BackgroundColor中的项目组合框WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做一个WPF并具有计算机上的avaible端口的列表的组合框。我想改变这些项目的颜色
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.
我的组合框是这些:
<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!!
推荐答案
要更改单个项目的背景颜色,您可以修改 ItemContainerStyle
,是这样的:
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>
这将设置背景颜色 ComboBoxItem
s到蓝色
This will set the background color on the ComboBoxItem
s to Blue
这篇关于BackgroundColor中的项目组合框WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!