Set the background color for selected items in a ListBox

I cannot set the background color for the selected item in the list. I do not want alternating colors in this example. I put them as a test and they work. The IsSelected trigger fires when the font size becomes bold and the foreground turns red. Setting the color brush backlight to SteelBlue does not provide the desired effect, as it disappears when the ListBox loses focus. Red and bold are held when the ListBox loses focus and this is what I want. I want the background color to be accepted and held for the selected item. Right now, the background for the selected items is white and held when the ListBox loses focus. Thank you for your help and I will do 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> 
+6
background wpf xaml listbox selecteditem
Aug 14 '11 at 20:30
source share
2 answers

You specify the SelectedItem background for the ListBox using 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> 
+14
Aug 14 2018-11-11T00:
source share
 <ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush> </ListBox.Resources> 

If you want this not to focus, you need to override the additional key:

 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush> 
+10
Aug 14 2018-11-11T00:
source share



All Articles