ListBox Selection Color - Rudi Grobler

ListBox Selection Color

While updating my Lab49 entry, I wanted to change the selection color of my default ListBox. This sounds like a simple task in WPF, you would think that!!!

Here is my problem:

I have a list box bound to a collection of stocks. I have a data template that determine how my list box items look! The DataTemplate is very simple, it has a StackPanel with a TextBlock that is bound to the company name!

So, what is the problem? The event IsSelected is only defined in ListBoxItem and I am using StackPanel. So how do I change the color of the selection indicator?

Well, the selection color is defined in SystemColors (HighlightBrushKey) so why not override it? Add the following to your ListBox's resource section

<ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFC8DAE8"/>
</ListBox.Resources>

Now you ListBox Selection Color will reflect the correct color!!!

Very cool...

PS. Beatriz shows a alternative method of how to retrieve the ListBoxItem here

kick it on DotNetKicks.com
Published Monday, March 03, 2008 8:58 AM by rudi
Filed under:

Comments

# ListBox Selection Color

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Monday, March 03, 2008 9:00 AM by DotNetKicks.com