How to apply alternate row style in .NET MAUI ListView (SfListView) ?
The .NET MAUI ListView (SfListView) allows the application of alternate row styling for items using IValueConverter.
XAML
Bind the IndexToColorConverter to the BackgroundColor property of the element loaded in the ItemTemplate. Pass the SfListView reference to find the index of the underlying object.
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ListViewMaui.MainPage" xmlns:local="clr-namespace:ListViewMaui" xmlns:listView="clr-namespace:Syncfusion.Maui.ListView;assembly=Syncfusion.Maui.ListView" xmlns:dataSource="clr-namespace:Syncfusion.Maui.DataSource;assembly=Syncfusion.Maui.DataSource"> <ContentPage.Resources> <ResourceDictionary> <local:IndexToColorConverter x:Key="IndexToColorConverter"/> </ResourceDictionary> </ContentPage.Resources> <ContentPage.Content> <listView:SfListView x:Name="listView" ItemSize="50" ItemsSource="{Binding ContactsInfo}" > <listView:SfListView.ItemTemplate> <DataTemplate> <Grid BackgroundColor="{Binding .,Converter={StaticResource IndexToColorConverter},ConverterParameter={x:Reference Name=listView}}" Padding="10,0,0,0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Text="{Binding ContactName}" VerticalOptions="End"/> <Label Grid.Row="1" Text="{Binding ContactNumber}" VerticalOptions="Start"/> </Grid> </DataTemplate> </listView:SfListView.ItemTemplate> </listView:SfListView> </ContentPage.Content> </ContentPage>
C#
Get the item index from the DataSource.DisplayItems property and return the color based on the index.
public class IndexToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var listview = parameter as SfListView; var index = listview.DataSource.DisplayItems.IndexOf(value); return index % 2 == 0 ? Colors.LightGray : Colors.Aquamarine; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Output
Download the complete sample on GitHub.
I hope you enjoyed learning how to apply alternate row style in .NET MAUI ListView
You can refer to our .NET MAUI ListView feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!