Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change WPF ComboBox SelectedText BackGround Color?

Tags:

c#

.net

mvvm

wpf

xaml

I have a Combobox in WPF-MVVM and i have styled the combobox with changes in the popdown box and textbox of combobox.

When i scroll the combobox listitem thier background is pink is what i have chnaged. But after selecting a item from the combobox list, the selected value in combobox item has blue background. which is the default for a combobbox in both Windows Form and WPF.

See the image for more details.

enter image description here

How can i change that selectedtext background color in the combobox textbox control

The combobox has

IsEditable=True property set

like image 804
Kishore Kumar Avatar asked Mar 28 '12 10:03

Kishore Kumar


People also ask

How do I change the background color in WPF?

Navigate to the Properties window, and click the Background drop-down arrow, and choose Red or another color in the color picker.

What is ComboBox in WPF?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


1 Answers

You can do this:

<ComboBox.Resources>
    <!--Selected color when the ComboBox is focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
    <!--Selected color when the ComboBox is not focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />

    <!--selected text-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow" />
</ComboBox.Resources>

(tested on ListBox but should work)

Another way is setting the ItemContainerStyle property of the ComboBox, and have a trigger depended on the current ComboBoxItem selection state:

<ComboBox>
  <ComboBox.Resources>
    <Style TargetType="TextBlock">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
          <Setter Property="Foreground" Value="White" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Resources>
  <ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem" x:Key="ContainerStyle">
      <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter Property="Background" Value="Red" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </ComboBox.ItemContainerStyle>    
</ComboBox>
like image 71
Shimmy Weitzhandler Avatar answered Sep 20 '22 21:09

Shimmy Weitzhandler