簡體   English   中英

BackgroundColor Items ComboBox WPF

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM