Search code examples
c#wpfcombobox

Is there any way to make the whole content of combobox item a different color?


I am looking for a way to change the color of the content of the entire combobox item, not just the text as in the image below:

current behavior setting the background

I am trying to achieve something like this:

desired behavior

The code I have so far and what I was able to find on the internet:

<ComboBox 
    ItemsSource="{Binding Items}"
        Height="49" Width="84">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" Background="Red"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Solution

  • You can set it by ItemContainerStyle to like this:

        <ComboBox 
    ItemsSource="{Binding Items}"
        Height="49" Width="84"
            DisplayMemberPath="Name">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Background" Value="Red" />
                    <Setter Property="BorderBrush" Value="Red" />
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    

    Do you wanna have border around every ComboBoxItem? You can set BorderBrush to Red too. Or hide it by BorderThickness to set it to "0"

    <Setter Property="BorderThickness" Value="0" />
    

    Hopefully this will help you.