I\'m trying to change the background of the selected item in a WPF ListBox.
I have attempted to implement a style for it, but for some reason it\'s not being applied
You specify the SelectedItem
Background
for a ListBox
with the SystemColors.HighlightBrushKey
(focused) and SystemColors.ControlBrushKey
(not focused)
<Style TargetType="{x:Type ListBox}">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="White"/>
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="White" />
</Style.Resources>
<!--...-->
</Style>
For anyone searching for this in the future, the accepted answer doesn't actually apply the colour when control is not focused for me. The following should be used instead which appears to work as intended.
<Style TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFFB733" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FFFFB733"/>
</Style.Resources>
</Style>