I ListBoxItem trying to set my selection color for ListBoxItem . I try this:

  <ListBox x:Name="AccountListBox" ItemsSource="{Binding}"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="LightSteelBlue"/> <Setter Property="Margin" Value="5"/> <Setter Property="Padding" Value="5"/> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Foreground" Value="White"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox> 

As a result, not selected Aytem looks as it should, but the selected one remains standard. What am I doing wrong? Google and everywhere asked so. And BorderThickness is set correctly, but there is no color.

  • Are you worried about the background selection when you move the mouse or when you click? - vitidev
  • @vitidev, in the future, you will need both when you hover and when selecting. but in this case at least with a click. - BwehaaFox

1 answer 1

The problem of color when clicking. Need to set the style

 <Style x:Key="SimpleListBoxItemStyle" TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" BorderThickness="0" SnapsToDevicePixels="true"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="LightSteelBlue" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> 

And for hover there is such a clever trick. I use it for a datagrid, but it may come up

 <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0000"/> </DataGrid.Resources>