Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / WPF
Tip/Trick

TIP: Change background of WPF Combobox

Rate me:
Please Sign up or sign in to vote.
4.95/5 (10 votes)
6 Nov 2012CPOL 87.5K   6   5
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:
 
C#
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:
 
XML
<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>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Peru Peru


Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

Comments and Discussions

 
QuestionSetting Disabled Background Color Pin
Jacquers5-May-16 3:48
Jacquers5-May-16 3:48 
GeneralAnd how can i change only the edit part of the Combobox?I t... Pin
olog20-Jan-12 7:06
olog20-Jan-12 7:06 
GeneralRe: I solved it: TextBox txtbx = (TextBox)comboBox1.Template.Fin... Pin
olog22-Jan-12 10:41
olog22-Jan-12 10:41 
GeneralRe: I solved it:TextBox txtbx = (TextBox)comboBox1.Template.Fin... Pin
yalenap12-Feb-14 13:23
yalenap12-Feb-14 13:23 
GeneralReason for my vote of 1 Bad Pin
gusbrasil10-Aug-10 6:06
gusbrasil10-Aug-10 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.