wpfxamllistviewbackgrounditemcontainerstyle

How to highlight row for IsSelected


I want to highlight the row when I select from my ListView but I cant get it to work. Can anyone look at what I have and tell me what I'm doing wrong? Another question is What about having a property in my ViewModel and setting the Background color based on the bool value, how can that be achieve?

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Green" />
            </Trigger>

            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

Solution

  • The problem here is the item template for the ListView automatically adds the "Selected highlight" of brush type SystemColors.HighlightBrushKey - the "true solution" would be to override the item template definition, but one way you can get what you're after here is something like this:

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>