59

Xamarin.Forms CollectionView Alternating Row Colors

 2 years ago
source link: https://xamarinhowto.com/xamarin-forms-collectionview-alternating-row-colors/?utm_campaign=xamarin-forms-collectionview-alternating-row-colors
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xamarin.Forms CollectionView Alternating Row Colors

Screen-Shot-2021-10-07-at-1.15.07-pm-600x638.png

Very quick Xamarin How-To this week with a simple converter. Alternating row colours in a CollectionView is quite often a design requirement and I wanted to create a simple, reusable converter to solve this.

The Converter

using System;
using System.Globalization;
using System.Linq;
using Xamarin.Forms;
namespace XhtCollectionViewAlternatingRowColor.Converters
    public class IndexToColorConverter : IValueConverter
        public Color EvenColor { get; set; }
        public Color OddColor { get; set; }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            var collectionView = parameter as CollectionView;
            return collectionView.ItemsSource.Cast<object>().ToList().IndexOf(value) % 2 == 0 ? EvenColor : OddColor;
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            throw new NotImplementedException();

Using the Converter in XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage Title="Alternating Row Colors"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:XhtCollectionViewAlternatingRowColor.Converters"
             xmlns:local="clr-namespace:XhtCollectionViewAlternatingRowColor"
             x:Class="XhtCollectionViewAlternatingRowColor.MainPage">
    <ContentPage.Resources>
        <converters:IndexToColorConverter x:Key="IndexToColorConverterGrid"
                                          EvenColor="#0D92DB"
                                          OddColor="White"/>
        <converters:IndexToColorConverter x:Key="IndexToColorConverterText"
                                          EvenColor="White"
                                          OddColor="Black"/>
    </ContentPage.Resources>
    <ContentPage.BindingContext>
        <local:MainViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <CollectionView x:Name="MyCollectionView"
                        ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid BackgroundColor="{Binding ., Converter={StaticResource IndexToColorConverterGrid}, ConverterParameter={x:Reference MyCollectionView}}">
                        <Label Text="{Binding .}"
                               TextColor="{Binding ., Converter={StaticResource IndexToColorConverterText}, ConverterParameter={x:Reference MyCollectionView}}"
                               Margin="20"/>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </ContentPage.Content>
</ContentPage>

There you go, nice and simple, easy to follow and reuse! As always full source code available here

About the Author: Toby Field

Toby has recently dived into the IT industry after graduating from QUT with a distinction in 2020. His passion & willingness to learn, drives his quality and career. Toby currently lives in Brisbane and is excited to see what the city has to offer in this growing IT hub.

Leave A Comment Cancel reply

Comment

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK