0
votes

I have a WPF Listview, and I have overridden the ListView.ItemTemplate to change the background color of the items on the ListViewItem.IsMouseOver event like so:

<AlternationConverter x:Key="BackgroundConverter">
    <SolidColorBrush>White</SolidColorBrush>
    <SolidColorBrush>
        <SolidColorBrush.Color>
            <Color A="242" R="242" G="242" B="242" />
        </SolidColorBrush.Color>
    </SolidColorBrush>
</AlternationConverter>

    <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

What I'm trying to achieve is to have a different color when hovered over the already selected item (which is yellow). So on all the items it will be a Blue hover-over, and on the selected Yellow item, it'll be green. I tried the below attempt, using MultiTrigger, but that didn't do the trick:

        <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ListBoxItem.IsMouseOver" Value="True"/>
                    <Condition Property="ListBoxItem.IsSelected" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="ListBoxItem.Background" Value="Green" />
            </MultiTrigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

Any ideas? Thanks.

1
This question is asked 2-3 times every day. Have a look at the following link: stackoverflow.com/questions/388232/…Fredrik Hedblad
apologies .. I always check to see if someone has asked the question, guess I didn't look close enough. Thanks for the link though.user350213

1 Answers

1
votes

Are the triggers are applied in order? It might work if you move the MultiTrigger to the bottom, then it will apply after the IsMouseOver Trigger.