Otto Vasken
Otto Vasken

Reputation: 67

WPF ComboBox, setting the background color of current item

In my WPF application I need to set the background color of my combobox.

Example

As you can see in the file attached, I set thw background color to blue as (in code behind):

_combobox.Background = Brushes.DodgerBlue;

I have also set the triggers in order to handle the event while selecting the items (set style for both ComboBox and ComboBoxItem:

<Style x:Key="CmbStyleItem" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border x:Name="gd" Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=Background}" Padding="4,6,2,2">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
                        <Setter TargetName="gd" Property="Background" Value="#E7E2E2" />
                        <Setter TargetName="gd" Property="TextElement.Foreground" Value="#000000"/>
                    </Trigger>
                    <Trigger Property="ComboBoxItem.IsSelected" Value="True">
                        <Setter TargetName="gd" Property="Background" Value="#D6D6D6" />
                        <Setter TargetName="gd" Property="TextElement.Foreground" Value="#000000" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type ComboBox}">
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="Height" Value="27" />
    <Setter Property="Padding" Value="4,6,2,2" />
    <Setter Property="FontFamily" Value="{StaticResource fntConsole}" />
    <Setter Property="Typography.Capitals" Value="AllSmallCaps"/>
    <Setter Property="FontSize" Value="13.55" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="ItemContainerStyle" Value="{DynamicResource CmbStyleItem}"/>
</Style> 

Basically I'm not able to set the background color of selected item, for example in the attachment, the item "Tav1800x650x18". Any hints?

Upvotes: 0

Views: 390

Answers (2)

Trung Messi
Trung Messi

Reputation: 342

Replace: Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=Background}"

To: Background="{TemplateBinding Background}"

Upvotes: 0

Frenchy
Frenchy

Reputation: 17007

if i understant your problem, you want to modify the background color of combobox when no selecteditem :

so you have this line <Setter Property="Background" Value="Transparent" /> which gives the default background color

you have to set Value with the color you want.

Or i missunderstand you.....

Upvotes: 0

Related Questions