[英]BackgroundColor Items ComboBox WPF
我正在做一个WPF,并有一个comboBox,其中包含计算机上可用端口的列表。 我想改变这些物品的颜色。
我的comboBox是这些:
<ComboBox HorizontalAlignment="Left" Margin="445,0,0,0" VerticalAlignment="Top" Width="120" Loaded="ComboBoxLoaded" SelectionChanged="ComboBoxSelectionChanged" Grid.Column="1" Background="#849096" Foreground="White"/>
这些是加载它的方法:
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;
}
我尝试了很多东西,但没有任何作用。 有人知道怎么做吗? 谢谢!!
要更改单个项目的背景颜色,可以更改ItemContainerStyle
,例如:
<ComboBox>
...
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background" Value="Blue" />
</Style>
</ComboBox.ItemContainerStyle>
...
</ComboBox>
这会将ComboBoxItem
上的背景颜色设置为Blue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.