Xamarin Forms Alternating Rows for ListView

When building a new Android app with Xamarin Forms, I needed to create alternating color rows for a ListView. I quickly came across a posting that made the job easy, but there was a problem with it. It used a Grid layout, and defined the layout twice identically, except for the background color. The problem with that is that it is an anti-pattern. Such are the things that make maintainability a nightmare. You may think that it’s just a few lines of code and not a big deal–maybe it’ll never even change. But the code can grow complex over time, and it would be easy to miss having to make a change twice to cover both different places.

With a bit of effort and help from Nirmal Subedi on StackOverflow, I found the right solution for creating alternating color rows on a ListView. The StackOverflow post that discusses this is here.

Create the DataTemplateSelector

First create the DataTemplateSelector. This is straight forward and is solely for rendering the correct DataTemplate based on whether the row is an even or an odd numbered row of the ListView.

using System.Collections;
using Xamarin.Forms;

namespace MyNamespace
    public class AlternateColorDataTemplateSelector : DataTemplateSelector
        public DataTemplate EvenTemplate { get; set; }
        public DataTemplate UnevenTemplate { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)

            ListView lv = container as ListView;
            if (lv != null)
                IList listItem = lv.ItemsSource as IList;
                int idx = listItem.IndexOf(item);
                return idx % 2 == 0 ? EvenTemplate : UnevenTemplate;
                return UnevenTemplate;

Create the Grid Layout

Define the row, together with all its bindings as a standalone Grid class object. An example is below. The binding to “Data” will need to be changed to whatever you are using for each row of the ListView.

The easiest way to do this is to add a new ContentView class, then change ContentView on the XAML and on the C# code-behind to “Grid”.

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
		<RowDefinition />
		<ColumnDefinition />
		<ColumnDefinition />
		Grid.Column="0" Text={Binding Name} />
		Grid.Column="1" Text={Binding Data} />

And the example code-behind to match:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyNamespace
    public partial class MasterGrid : Grid
        public MasterGrid()

Putting it all together

Which now leaves putting it all together on your PageView:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             Title="Sample Alternating Color ListView">
        <DataTemplate x:Key="evenTemplate">
                <local:MasterGrid Background="Red" />
        <DataTemplate x:Key="unevenTemplate">
                <local:MasterGrid Background="Blue" />
        <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
                EvenTemplate="{StaticResource evenTemplate}"
                UnevenTemplate="{StaticResource unevenTemplate}" />
                ItemTemplate="{StaticResource alternateColorDataTemplateSelector}"
                ItemsSource="{Binding MyList}" />

One thought on “Xamarin Forms Alternating Rows for ListView

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: