Question

Is it possible to highlight every second row in list view with some color? For example I have a listview with data from data table, bound to it. So I want to leave the first row background to be white and then second to be blue (for example), then the third would be white, the fourth blue again and so on. Also I would like this effect to continue automatically when I add new items to list view.

Was it helpful?

Solution

[From @Tim's comment you could have Googled and got a MS solution from here or looked at @Vijay's link...]

For a different approach. First create a style element in your ResourceDictionary or if you don't have a ResourceDictionary drop it on the page itself

<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
     <Style.Triggers>
         <!-- setting up triggers for alternate background colors -->
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
             <Setter Property="Background" Value="LightGray"></Setter>
         </Trigger>
         <Trigger Property="ItemsControl.AlternationIndex" Value="2">
             <Setter Property="Background" Value="White"></Setter>
         </Trigger>
     </Style.Triggers>
     <!-- setting row height here --> 
     <Setter Property="Height" Value="30" />
</Style>

Now set the ItemContanerStyle on the actual ListView and set the AlternationCount attribute):

<ListView Name="recordContainer" ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" AlternationCount="2" >
   <ListView.View>
      <GridView>
         <!-- Just a few sample columns -->         
         <GridViewColumn Header="aField" />
         <GridViewColumn Header="anotherField" />
         <GridViewColumn Header="yetAnotherField" />
      </GridView>
   </ListView.View>
   <!-- Whatever you might have in here -->
</ListView>

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top