Alternate Background for ListViewItems

This sample shows how to create a ListView control that uses a GridView to display a list of dates that are defined as DateTime objects. Items in this example have alternate background.


Key Step 1. Restyle ListViewItem, and set its Background by converter in the new style.


    <l:BackgroundConvertor x:Key="myConverter"/>

    <Style x:Key="myItemStyle" TargetType="{x:Type ListViewItem}">

      <Setter Property="Background">


          <Binding RelativeSource="{RelativeSource Self}" Converter="{StaticResource myConverter}"/>





<ListView Name="lv" ItemContainerStyle="{StaticResource myItemStyle}" />


Key Step 2. Implement BackgroundConvertor. We need the index of a ListViewItem to determine its background. int ItemContainerGenerator.IndexFromContainer(DependencyObject container) method can tell the index of a ListViewItem.


public sealed class BackgroundConvertor : IValueConverter


    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)


        ListViewItem item = (ListViewItem)value;

        ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;

        // Get the index of a ListViewItem

        int index = listView.ItemContainerGenerator.IndexFromContainer(item);


        if (index % 2 == 0)


            return Brushes.LightBlue;




            return Brushes.White;




    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)


        throw new NotSupportedException();




Key Step 3. We’ve almost finished the sample, and it works well in sorting/grouping. However, when one item is removed from the ListView, you may find two adjacent rows have the same background color. That's because the background of all the other items will not be updated automatically after removing/inserting. Therefore, some extra code is needed to refresh the items after the inserting/removing operation.


private void Remove(object sender, RoutedEventArgs e)




    // Must refresh all the items to update the background

    ICollectionView dataView =





This sample is based on the February CTP.

Comments (2)
  1. MichaelG says:

    Great samples – keep them coming.

    Can you shed any light on what happened to the DatePicker and Month Calendar controls.  If they’ve gone for good, is there any chance you can come up with a sample DatePicker?

    Keep up the good work.


Comments are closed.

Skip to main content