WPF ListBox and SelectedItem’s BackColor

In our application we use black as background color everywhere and white or light gray as foreground color.  I was working on a ListBox that has very complicated item’s layout.  Essentially there are about 8 different controls contained within each item.  ListBox is simply used as a container to view the data, we really do not need to know which item is highlighted by the user.  Default background color for ListBox’s highlighted item is a system color, called HighlightBrushKey.  In my case (on my machine) it is blue.  Highlighted items looks UGLY with this color schema.  So, I wanted to simply remove the highlight altogether.  To do so, I defined a custom style as so

<UserControl.Resources>

    <Style x:Key="customListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
        </Style.Resources>
    </Style>

</UserControl.Resources>

This style is replacing default system color with transparent color, essentially removing the highlights.  To use this style in the ListBox, I did the following:

<ListBox ItemsSource="{Binding Path=ClientProductGroups}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ItemContainerStyle="{StaticResource customListBoxItemStyle}">

Leave a Reply

Your email address will not be published. Required fields are marked *