Wednesday, June 5, 2013

Keeping the Selection Color in a WPF Listbox When it Loses Focus

 

There are a lot of blog posts out there telling you to change the the ControlBrush resource of a listbox in order to maintain the selection color when the listbox item loses focus. This does not work.

If you want to set the color of the background when an item is selected, you need to change the HighlightBrush. If you want that color to persist even after the item loses focus, you need to change the InactiveSelectionHighlightBrush.

Hope this helps. Happy Coding!

 

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


2 comments: