Hello,
Welcome to our Microsoft Q&A platform!
However, I'm looking for a way to apply this to all the ListViews of my application
We cannot get the item index in the ListView
class. To achieve this function, try create a custom ViewCell
class instead.
Check the code:
public class CustomViewCell : ViewCell
{
public Color EvenColor { get; set; }
public Color UnevenColor { get; set; }
protected override void OnAppearing()
{
base.OnAppearing();
if (!(Parent is ListView listView))
return;
int index;
if (listView.IsGroupingEnabled)
{
index = listView.TemplatedItems.GetGroupAndIndexOfItem(BindingContext).Item2;
}
else
{
index = listView.TemplatedItems.IndexOf(this);
}
if (index != -1)
{
this.View.BackgroundColor = index % 2 == 0 ? EvenColor : UnevenColor;
}
}
}
Specify a value to the 'EvenColor' and 'UnevenColor' property in the page.xaml.
<ListView x:Name="listview" ...>
<ListView.ItemTemplate>
<DataTemplate>
<local:CustomViewCell EvenColor="Red" UnevenColor="Blue">
...
</local:CustomViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.