WPF Change Background color of a Combobox

Tvd

In my WPF app I just want to change the background color of the Combo box. I don't mean the dropdown, I want is just whatever item is selected a background is set. Like setting the background of a button - when the control is displayed on screen it should have LightYellow background. That's it. I searched a lot on net, but everywhere could find solutions for drop down background colors. I tried applying SolidColorBrush and Style.Triggers to the TextBlock of Combobox, but no success as wanted. By adding SolidColorBrush lines, I got my dropdown background set, but that's not what I am looking for. My code is :

<ComboBox ItemsSource="{Binding MtrCm}" SelectedValue="{Binding WellboreDiameter_Unit, Mode=TwoWay}" Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,26,249,0" x:Name="cboWellDiameter" VerticalAlignment="Top" Width="120"   Background="LightYellow"  >
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Resources>
</ComboBox>

Can anyone help me set he background of the desired component that am looking for.

Thanks

NullReferenceException

Try this

 <Window.Resources>  //Put this resourse n Window.Resources or UserControl.Resources
   <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
      <GradientBrush.GradientStops>
         <GradientStopCollection>
            <GradientStop Color="#FFDC3939" Offset="0.0"/>
            <GradientStop Color="#FFE80E0E" Offset="1.0"/>
         </GradientStopCollection>
      </GradientBrush.GradientStops>
   </LinearGradientBrush>

    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFFBE618" />

    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>
            <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2"
  Background="{StaticResource NormalBrush}"
  BorderThickness="1" />
            <Border 
  Grid.Column="0"
  CornerRadius="2,0,0,2" 
  Margin="1" 
  Background="{StaticResource WindowBackgroundBrush}" 
  BorderThickness="0,0,1,0" />
            <Path 
  x:Name="Arrow"
  Grid.Column="1"     
  HorizontalAlignment="Center"
  VerticalAlignment="Center"
  Data="M 0 0 L 4 4 L 8 0 Z"/>
        </Grid>
    </ControlTemplate>

    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
        <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
    </ControlTemplate>


<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
  <Setter Property="Template">
   <Setter.Value>
     <ControlTemplate TargetType="ComboBox">
      <Grid>
       <ToggleButton 
         Name="ToggleButton" 
         Template="{StaticResource ComboBoxToggleButton}" 
         Grid.Column="2" 
         Focusable="false"
         IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
         ClickMode="Press">
      </ToggleButton>
      <ContentPresenter
        Name="ContentSite"
        IsHitTestVisible="False" 
        Margin="3,3,23,3"
        VerticalAlignment="Center"
        HorizontalAlignment="Left" />
       <TextBox x:Name="PART_EditableTextBox"
         Style="{x:Null}" 
         Template="{StaticResource ComboBoxTextBox}" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Center" 
         Margin="3,3,23,3"
         Focusable="True" 
         Background="Transparent"
         Visibility="Hidden"
         IsReadOnly="{TemplateBinding IsReadOnly}"/>
      <Popup 
        Name="Popup"
        Placement="Bottom"
        IsOpen="{TemplateBinding IsDropDownOpen}"
        AllowsTransparency="True" 
        Focusable="False"
        PopupAnimation="Slide">
        <Grid 
          Name="DropDown"
          SnapsToDevicePixels="True"                
          MinWidth="{TemplateBinding ActualWidth}"
          MaxHeight="{TemplateBinding MaxDropDownHeight}">
           <Border 
            x:Name="DropDownBorder"
            Background="{StaticResource WindowBackgroundBrush}"
            BorderThickness="1"/>
           <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
           </ScrollViewer>
          </Grid>
         </Popup>
        </Grid>
       </ControlTemplate>
      </Setter.Value>
     </Setter>
    <Style.Triggers>
   </Style.Triggers>
  </Style>
 </Window.Resources>
 <Grid>
    <ComboBox HorizontalAlignment="Left" Margin="256,57,0,0" VerticalAlignment="Top" Width="120">
    </ComboBox>

 </Grid>

Here is the complete style that you can change : http://msdn.microsoft.com/en-us/library/ms752094%28v=VS.85%29.aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change the background color of a WPF editable ComboBox programmatically

From Dev

Change the background color of a WPF editable ComboBox programmatically

From Dev

Change border color of Combobox in WPF

From Dev

How change the background color of the dropdown panel of Combobox

From Dev

Use ComboBox to change the background color of panel

From Dev

WPF combobox hover change color template

From Dev

WPF change cell background color based on content

From Dev

Change Button Background color on EventTrigger in WPF

From Dev

WPF: How to change TransitioningContentControl background color?

From Dev

Change BackGround Color from database(WPF)?

From Dev

WPF change cell background color based on content

From Dev

WPF Change focused TextBox background color

From Dev

How to change background color in ttk.Combobox's listview?

From Dev

Change color of Selected-Unfocused ComboBox Item in wpf

From Dev

WPF: Change DataGrid cell/row background color dynamically at runtime

From Dev

Change Background Color of SelectedItem in ListBox in .NET 4.5 WPF

From Dev

How to change ViewPort3D background color in WPF?

From Dev

How to change the background color of a Passwordbox for WPF/C#

From Dev

Change WPF treeViewItem Background color when selected but lost focus

From Dev

WPF TabControl, change the background color of the TabItem with C# codes

From Dev

Change background color of WPF DataGrid Top Left Corner Header

From Dev

C# WPF - Dynamically Change Text Color based on Background Image

From Dev

Change background color of WPF DataGrid Top Left Corner Header

From Dev

How do I change dynamically a background color of a WPF:s button

From Dev

Change wpf data grid row background color when checkbox is checked

From Dev

Override IsMouseOver and change background color based on logic in WPF Button

From Dev

Moving strokes between InkCanvas with background color change of only the strokes? (WPF)

From Dev

WPF Change datagrid cell background color using a converter

From Dev

WPF ComboBox Dropdropdown SelectedItem Background

Related Related

  1. 1

    Change the background color of a WPF editable ComboBox programmatically

  2. 2

    Change the background color of a WPF editable ComboBox programmatically

  3. 3

    Change border color of Combobox in WPF

  4. 4

    How change the background color of the dropdown panel of Combobox

  5. 5

    Use ComboBox to change the background color of panel

  6. 6

    WPF combobox hover change color template

  7. 7

    WPF change cell background color based on content

  8. 8

    Change Button Background color on EventTrigger in WPF

  9. 9

    WPF: How to change TransitioningContentControl background color?

  10. 10

    Change BackGround Color from database(WPF)?

  11. 11

    WPF change cell background color based on content

  12. 12

    WPF Change focused TextBox background color

  13. 13

    How to change background color in ttk.Combobox's listview?

  14. 14

    Change color of Selected-Unfocused ComboBox Item in wpf

  15. 15

    WPF: Change DataGrid cell/row background color dynamically at runtime

  16. 16

    Change Background Color of SelectedItem in ListBox in .NET 4.5 WPF

  17. 17

    How to change ViewPort3D background color in WPF?

  18. 18

    How to change the background color of a Passwordbox for WPF/C#

  19. 19

    Change WPF treeViewItem Background color when selected but lost focus

  20. 20

    WPF TabControl, change the background color of the TabItem with C# codes

  21. 21

    Change background color of WPF DataGrid Top Left Corner Header

  22. 22

    C# WPF - Dynamically Change Text Color based on Background Image

  23. 23

    Change background color of WPF DataGrid Top Left Corner Header

  24. 24

    How do I change dynamically a background color of a WPF:s button

  25. 25

    Change wpf data grid row background color when checkbox is checked

  26. 26

    Override IsMouseOver and change background color based on logic in WPF Button

  27. 27

    Moving strokes between InkCanvas with background color change of only the strokes? (WPF)

  28. 28

    WPF Change datagrid cell background color using a converter

  29. 29

    WPF ComboBox Dropdropdown SelectedItem Background

HotTag

Archive