Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListView control and I'm trying to create a style to; color alternating rows, which works, color the rows when hovering and selecting.

The hovering and selecting is the problem, the default behavior does not allow me to change the colors.

XML
<ListView Name="AttachmentListView" Margin="2 5 0 10"
          MaxHeight="360"
          ItemsSource="{Binding Path=AttachmentView}"
          SelectedItem="{Binding SelectedAttachment}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding AttachmentTabToolbarClickCommand}"
                     CommandParameter="{Binding ElementName=AttachmentListView, Path=SelectedItem}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="32"/>
                </Grid.ColumnDefinitions>
                <StackPanel TextElement.FontFamily="Monserrat" TextElement.FontSize="14">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock VerticalAlignment="Center"
                                   Text="[ Attachment Type: "/>
                        <TextBlock VerticalAlignment="Center"
                                   Text="{Binding AttachmentType}" Foreground="{DynamicResource MahApps.Brushes.Text}"/>
                        <TextBlock Text=" ] "/>
                        <TextBlock VerticalAlignment="Center"
                                   Text="    < Description: "/>
                        <TextBlock VerticalAlignment="Center"
                                   Text="{Binding Description}" Foreground="{DynamicResource MahApps.Brushes.Text}"/>
                        <TextBlock Text=" > "/>
                    </StackPanel>
                    <TextBlock Name="filetb" FontWeight="Bold" Text="{Binding Path=Filename}" Foreground="{DynamicResource MahApps.Brushes.Text}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="BorderBrush" Value="#b1b0a3"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                    <Setter Property="Background" Value="#d8d7d1" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                    <Setter Property="Background" Value="#ecebe8" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="#b1b0a3"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="#b1b0a3"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


What I have tried:

I'm thinking it can be solved using a control template but I'm not sure how to implement. I've attempted a few different variations but had no luck, the ListView failed to display the items when I tried.

Any help appreciated.
Posted
Comments
Jo_vb.net 31-Mar-24 17:59pm    
Are you sure your resource dictionary is being included properly?
In App.xml ?
Mike Hankey 31-Mar-24 18:05pm    
Yes, I've been searching for some time on this problem and finally found the answer. Ended up it was a ControlTemplate problem, as I thought and it is working now. Thanks for the feedback.

1 solution

AlternationIndex and IsSelected are both properties of a ListViewItem, so a standard Style.Trigger will not work.

You need to use a MultiTrigger Class (System.Windows) | Microsoft Learn[^]. Examples are provided with that link, however here is how I would use it (untested):

XML
<MultiTrigger>
	<MultiTrigger.Conditions>
	   <Condition Property="IsSelected" Value="True"/>
	   <Condition Property="ItemsControl.AlternationIndex" Value="0"/>
	</MultiTrigger.Conditions>
	<Setter Property="Background" Value="LightGreen"/>
 </MultiTrigger>
 <MultiTrigger>
	<MultiTrigger.Conditions>
	   <Condition Property="IsSelected" Value="True"/>
	   <Condition Property="ItemsControl.AlternationIndex"
				  Value="1"/>
	</MultiTrigger.Conditions>
	<Setter Property="Background" Value="LightBlue"/>
 </MultiTrigger>


Enjoy!
 
Share this answer
 
Comments
Mike Hankey 31-Mar-24 21:38pm    
Thanks Graeme that's what I ended up doing.

I've been using WPF in a couple of apps but not really learning much about Styles and Templates, only simple stuff. I ordered a book today and plan on really learning it so hopefully won't be asking many more rookie questions. :)
Graeme_Grant 31-Mar-24 22:24pm    
All good. Glad to hear that you resolved it. My solution should hopefully help others too. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900