Skip to content

Instantly share code, notes, and snippets.

@tomcurran
Last active September 1, 2025 17:03
Show Gist options
  • Select an option

  • Save tomcurran/435fe7f76376bfb823d79d3158b75c32 to your computer and use it in GitHub Desktop.

Select an option

Save tomcurran/435fe7f76376bfb823d79d3158b75c32 to your computer and use it in GitHub Desktop.
MAUI ListView alternative row background colour
namespace SomeNamespace.Converters
{
using System;
using System.Globalization;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Syncfusion.Maui.ListView;
public class AlternativeRowColorConverter : BindableObject, IValueConverter
{
public static readonly BindableProperty PrimaryColorProperty =
BindableProperty.Create(nameof(PrimaryColor), typeof(string), typeof(AlternativeRowColorConverter), null);
public static readonly BindableProperty SecondaryColorProperty =
BindableProperty.Create(nameof(SecondaryColor), typeof(string), typeof(AlternativeRowColorConverter), null);
public string PrimaryColor
{
get => (string)GetValue(PrimaryColorProperty);
set => SetValue(PrimaryColorProperty, value);
}
public string SecondaryColor
{
get => (string)GetValue(SecondaryColorProperty);
set => SetValue(SecondaryColorProperty, value);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (Equals(value, null))
{
return GetNamedResourceColor(PrimaryColor);
}
var listview = parameter as SfListView;
return listview.DataSource.DisplayItems.IndexOf(value) % 2 == 0 ? GetNamedResourceColor(PrimaryColor) : GetNamedResourceColor(SecondaryColor);
}
catch (Exception ex)
{
return GetNamedResourceColor(PrimaryColor);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
private static Color GetNamedResourceColor(string resourceName)
{
Application.Current!.Resources.TryGetValue(resourceName, out var resource);
if (resource != null)
{
return (Color)resource;
}
var black = "0,0,0";
return Color.FromArgb(black);
}
}
}
<Grid>
<Maui:SfListView
x:Name="ListView">
<Maui:SfListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.BackgroundColor>
<Binding
Path="."
ConverterParameter="{x:Reference ListView}">
<Binding.Converter>
<converters:AlternativeRowColorConverter
PrimaryColor="PrimaryBackgroundColor"
SecondaryColor="SecondaryBackgroundColor" />
</Binding.Converter>
</Binding>
</Grid.BackgroundColor>
</Grid>
</DataTemplate>
</Maui:SfListView.ItemTemplate>
</Maui:SfListView>
</Grid>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment