Change background color for selected ListBox item

WpfStyles

Wpf Problem Overview


This is my XAML so far.

<ScrollViewer Grid.Column="1" Grid.RowSpan="2">

	<ListBox   Background="Black" ItemsSource="{Binding Path=ActiveLog}" >
		<ListBox.ItemTemplate>
			<DataTemplate>
				<Grid Background="Black">
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="200"></ColumnDefinition>
						<ColumnDefinition Width="*"></ColumnDefinition>
					</Grid.ColumnDefinitions>
					<Grid.RowDefinitions>
						<RowDefinition></RowDefinition>
						<RowDefinition></RowDefinition>
					</Grid.RowDefinitions>
					<TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">
						<TextBlock >Date:</TextBlock>
						<TextBlock  Text="{Binding Path=LogDate}"/>
					</TextBlock>
					<TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">
						<TextBlock >Severity:</TextBlock>
						<TextBlock  Text="{Binding Path=Severity}"/>
					</TextBlock>
					<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>
				</Grid>
			</DataTemplate>
		</ListBox.ItemTemplate>
		<ListBox.Template>
			<ControlTemplate>
				<StackPanel Background="Black" IsItemsHost="True" >
				</StackPanel>
			</ControlTemplate>
		</ListBox.Template>

	</ListBox>
</ScrollViewer>

The only problem is that the selected item has a blue box to the right. I assume there is a way to change the selection color, but I can't find it.

Wpf Solutions


Solution 1 - Wpf

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

and

<ListBox ItemsSource="{Binding Path=FirstNames}"
         ItemContainerStyle="{StaticResource myLBStyle}">  

You just override the style of the listboxitem (see the: TargetType is ListBoxItem)

Solution 2 - Wpf

Or you can apply HighlightBrushKey directly to the ListBox. Setter Property="Background" Value="Transparent" did NOT work. But I did have to set the Foreground to Black.

<ListBox  ... >
	<ListBox.ItemContainerStyle>
		<Style TargetType="ListBoxItem">
			<Style.Triggers>
				<Trigger Property="IsSelected" Value="True" >
					<Setter Property="FontWeight" Value="Bold" />
					<Setter Property="Background" Value="Transparent" />
					<Setter Property="Foreground" Value="Black" />
				</Trigger>
			</Style.Triggers>
			<Style.Resources>
				<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
			</Style.Resources>
		</Style>                
	</ListBox.ItemContainerStyle>
</ListBox>

Solution 3 - Wpf

You need to use ListBox.ItemContainerStyle.

ListBox.ItemTemplate specifies how the content of an item should be displayed. But WPF still wraps each item in a ListBoxItem control, which by default gets its Background set to the system highlight colour if it is selected. You can't stop WPF creating the ListBoxItem controls, but you can style them -- in your case, to set the Background to always be Transparent or Black or whatever -- and to do so, you use ItemContainerStyle.

juFo's answer shows one possible implementation, by "hijacking" the system background brush resource within the context of the item style; another, perhaps more idiomatic technique is to use a Setter for the Background property.

Solution 4 - Wpf

I had to set both HighlightBrushKey and ControlBrushKey to get it to be correctly styled. Otherwise, whilst it has focus this will correctly use the transparent HighlightBrusKey. Bt, if the control loses focus (whilst it is still highlighted) then it uses the ControlBrushKey.

<Style.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</Style.Resources>

When Using .Net 4.5 and above, use InactiveSelectionHighlightBrushKey instead of ControlBrushKey:

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

Hope this helps someone out.

Solution 5 - Wpf

You have to create a new template for item selection like this.

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Border
                BorderThickness="{TemplateBinding Border.BorderThickness}"
                Padding="{TemplateBinding Control.Padding}"
                BorderBrush="{TemplateBinding Border.BorderBrush}"
                Background="{TemplateBinding Panel.Background}"
                SnapsToDevicePixels="True">
                <ContentPresenter
                    Content="{TemplateBinding ContentControl.Content}"
                    ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Solution 6 - Wpf

If selection is not important, it is better to use an ItemsControl wrapped in a ScrollViewer. This combination is more light-weight than the Listbox (which actually is derived from ItemsControl already) and using it would eliminate the need to use a cheap hack to override behavior that is already absent from the ItemsControl.

In cases where the selection behavior IS actually important, then this obviously will not work. However, if you want to change the color of the Selected Item Background in such a way that it is not visible to the user, then that would only serve to confuse them. In cases where your intention is to change some other characteristic to indicate that the item is selected, then some of the other answers to this question may still be more relevant.

Here is a skeleton of how the markup should look:

	<ScrollViewer>
		<ItemsControl>
			<ItemsControl.ItemTemplate>
				<DataTemplate>
                    ...
				</DataTemplate>
			</ItemsControl.ItemTemplate>
		</ItemsControl>
	</ScrollViewer>

Solution 7 - Wpf

I've tried various solutions and none worked for me, after some more research I've found a solution that worked for me here

https://gist.github.com/LGM-AdrianHum/c8cb125bc493c1ccac99b4098c7eeb60

   <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
                 Width="200" Height="250"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>Hi</ListBoxItem>
        </ListBox>

I posted it here, as this is the first google result for this problem so some others may find it useful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan AllenView Question on Stackoverflow
Solution 1 - WpfjuFoView Answer on Stackoverflow
Solution 2 - WpfpaparazzoView Answer on Stackoverflow
Solution 3 - WpfitowlsonView Answer on Stackoverflow
Solution 4 - WpfRichSView Answer on Stackoverflow
Solution 5 - WpfDuncanView Answer on Stackoverflow
Solution 6 - Wpfuser3308241View Answer on Stackoverflow
Solution 7 - WpfCiucaSView Answer on Stackoverflow