0

How can I style the selected items in this listbox?

<ListBox x:Name="AssetTypeListBox" SelectionMode="Multiple">
    <ListBox.ItemsPanel >
        <ItemsPanelTemplate >
             <UniformGrid Columns="6"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel> 
</ListBox> 

I have tried

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

and

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#F15025"/>

But they don't seem to affect the selected items as I would expect. Thanks.

1 Answers1

0

If you just want to change the colour of the selection, then the easiest way is to set the solidcolorbrush as a resource on your Listbox:

<ListBox x:Name="AssetTypeListBox" SelectionMode="Multiple">
    <ListBox.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F15025"/>
         <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#F15025"/>
    </ListBox.Resources>
    <ListBox.ItemsPanel >
        <ItemsPanelTemplate >
             <UniformGrid Columns="6"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel> 
</ListBox> 

If you're wanting to customise the style of the selection of an item in the listbox, such as changing the selection colour or completely restyling the look of a selected item, you need to look at the ItemContainerStyle property.

The ItemsPanelTemplate doesn't affect this - it simply sets the ItemsControl type that should layout and present the items - such as a StackPanel or UniformGrid.

The basic hierarchy (with a pinch of salt) of a ListBox is:

  • ListBox
    • ItemsPanelTemplate (StackPanel by default)
      • ItemsContainerStyle (Selection highlighting is here)
        • ItemTemplate (You define the content of your list item here)

The differences between ItemTemplates and ItemsContainerStyle are covered here: What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?

olitee
  • 1,683
  • 10
  • 12