14

I need to change the highlight color of a ComboBox's selected item in the popup list. I've found several tutorials explaining how to do this, but all of them either use Blend, which I do not have and cannot obtain, or involve changing the system default colors--which seems like a hack to me.

Can someone point me to the template I need to override, or tell me the property I need to set?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Klay
  • 1,971
  • 5
  • 26
  • 40

2 Answers2

24

Override the SystemColors.HighlightBrushKey (and SystemColors.HighlightTextBrushKey if you want):

<ComboBox>
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Red</SolidColorBrush>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}">Blue</SolidColorBrush>
    </ComboBox.Resources>
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
</ComboBox>
josh2112
  • 829
  • 6
  • 22
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Once again, isn't there a way to do this that doesn't involve changing the system colors? – Klay Aug 14 '09 at 15:56
  • I keep finding references to a SelectionBoxItemTemplate. Is this what controls the highlighted item in the popup? – Klay Aug 14 '09 at 15:59
  • 3
    You're not changing the system colors - you're merely overriding them at the scope of the ComboBox. – Kent Boogaart Aug 14 '09 at 19:32
  • I guess I'm looking for the template I would change, as it seems like that would be the "correct" way to do it. Would this work for setting the brush to be a gradient? What if I wanted to display an icon next to the selected item? In any case, I do appreciate the help. – Klay Aug 17 '09 at 13:22
  • 1
    The "correct" way depends on exactly what you're trying to achieve ;) It is the ComboBoxItem template that reaches out and grabs these brush resources, so you would have to re-template that if you decide to go that route. Any brush (including gradient) can be used in the example above. If you want your icon outside the bounds of the selection box you could probably fudge a solution without re-templating but re-templating would be the better solution. – Kent Boogaart Aug 17 '09 at 14:40
  • Sorry to ask so many questions, but how do I include your XAML in a ControlTemplate for the ComboBox? I can't seem to figure out how to include a resources section in which to put it. – Klay Aug 18 '09 at 13:07
  • 4
    BTW it look this is broken in windows 8. – Joel Barsotti Sep 14 '12 at 17:35
0

I have created a template for Combobox here :

http://wpfstyles.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=31388#DownloadId=78720

Thanks, Vikas

  • 1
    How'd you get or knew about the "Panel" in in ComboBoxItem style ? – Jayson Ragasa Jan 30 '13 at 23:33
  • @JaysonRagasa, I realise that this is really old now, but in case you or other readers never found out, you can access the default WPF control templates in Visual Studio: [How to Extract Default Control Template In Visual Studio?](https://stackoverflow.com/questions/8825030/how-to-extract-default-control-template-in-visual-studio). – Sheridan Dec 18 '17 at 09:12