I want to change the background color of ListBoxItem
After search I decide to use this
<ListBox>
<ListBox.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Blue" />
</ListBox.Resources>
<TextBlock>fdfsdf1</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf5</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf4</TextBlock>
</ListBox>
When a listboxitem is focused, the background is blue as expected, but when the selected listboxitem loses focus, the background turns gray. How can I make the background remain blue when it loses focus?
if you mean just when its selected but inactive try InactiveSelectionHighlightBrushKey
<ListBox.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Blue" />
</ListBox.Resources>
Try this
<ListBox>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Blue" />
</Style>
</ListBox.Resources>
<TextBlock>fdfsdf1</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf5</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf4</TextBlock>
</ListBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With