Listbox item does not change background after selection in WPF

98 Views Asked by At

I followed some answers online but it still fails. In the <Window.Resources> tag I add the following style

        <Style TargetType="ListBoxItem" x:Key="ContainerStyle">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True" >
                    <Setter Property="Background" Value="#fff291"/>
                    <Setter Property="Foreground" Value="#000"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="False" >
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="Background" Value="#013268"/>
                </Trigger>
            </Style.Triggers>
        </Style>

After I launch the program and select items, the foreground changes but the background remains unchanged. The color is the same as the one when the mouse hovers over the item.

enter image description here

As you can see, the first one is selected and the fourth one has a mouse hovering over it. The background color of the first one does not change as expected. XAML for the Listbox is as follows:

        <ListBox x:Name="catDocListView" Grid.Column="0" Grid.RowSpan="2" Margin="16" ItemContainerStyle="{StaticResource ContainerStyle}">    
            <ListBoxItem>ListBox Item #1</ListBoxItem>
            <ListBoxItem>ListBox Item #2</ListBoxItem>
            <ListBoxItem>ListBox Item #3</ListBoxItem>
            <ListBoxItem>ListBox Item #4</ListBoxItem>
        </ListBox>

Could someone give me a hint?

0

There are 0 best solutions below