65.9K
CodeProject is changing. Read more.
Home

TIP: Change background of WPF Combobox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.95/5 (8 votes)

May 30, 2010

CPOL
viewsIcon

90159

How to change the popup zone of a WPF Combobox programmatically without restyling

The Background property will just change the edit and drop arrow area of a WPF Combobox.
To change other colors like popup background and highlight color, you have to add some brushes to the resource dictionary, mapping to the system colors:
 
var combo = new Combobox(); 
combo.Background = Brushes.Yellow;
combo.Foreground = Brushes.Blue;
combo.Resources.Add(SystemColors.WindowBrushKey, Brushes.Yellow);
combo.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Red);
 
This is equivalent to XAML code:
 
<Combobox Background="Yellow" Foreground="Blue">
   <Combobox.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
   </Combobox.Resources>
</Combobox>