7

I cannot set the background color for the selected item on a list box. I don't want the alternating colors in this example. I put them in as a test and they work. Trigger IsSelected is firing as the fontweight goes to bold and the foreground goes to red. Setting highlight color brush to SteelBlue does not achieve the desired effect as it goes away when the ListBox loses focus. Red and bold does hold when the ListBox loses focus and is what I want. I want the background color to take and hold for the selected item. Right now the background for the selected items is white and holds when the ListBox loses focus. Thanks for your help and I will test any proposed fix.

 <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2" ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left" PresentationTraceSources.TraceLevel="High" AlternationCount="2" > <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="VerticalContentAlignment" Value="Center" /> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Background" Value="LightGreen"></Setter> </Trigger> <Trigger Property="ItemsControl.AlternationIndex" Value="1"> <Setter Property="Background" Value="LightPink"></Setter> </Trigger> <Trigger Property="IsSelected" Value="True" > <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Background" Value="SteelBlue" /> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> </Style.Resources> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

2 Answers 2

14

You specify the SelectedItem Background for a ListBox with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)

<Style.Resources> <!-- Background of selected item when focussed --> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/> <!-- Background of selected item when not focussed --> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGreen" /> </Style.Resources> 
Sign up to request clarification or add additional context in comments.

2 Comments

Worth mentioning that as of 1/2015, this has no effect on the selection highlight colors of System.Windows.Controls.ListBox with the standard theme. Triggers work for Foreground but not Background. You have to retemplate ListBoxItem.
Working (but messy) solution as of 2020: stackoverflow.com/questions/64361138/…
10
<ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush> </ListBox.Resources> 

If you want this to apply out of focus as well you need to override an additional key:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush> 

4 Comments

I had to accept the other as the syntax as a little cleaner but I gave you a +1 thanks
@BalamBalam: "Clean" is debatable, and my answer was seven minutes faster, oh well, thanks anyway...
@H.B. Sorry, didn't actually noticed that you had answered pretty much the same thing before me.. Here's +1 anyway
@Meleak: No worries really, happens, in fact we had a similar case elsewhere before if you remember that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.