IrA*_*App 4 .net c# xaml visual-studio
我正在做一个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"/>
Run Code Online (Sandbox Code Playgroud)
这些是加载它的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,但没有任何作用.有人知道怎么做吗?谢谢!!
要更改单个项目的背景颜色,您可以更改ItemContainerStyle,例如:
<ComboBox>
...
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background" Value="Blue" />
</Style>
</ComboBox.ItemContainerStyle>
...
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
这将在ComboBoxItems 上设置背景颜色Blue