Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ViewCell background color changes according to cell index.

$
0
0

Hello Xamarin folks,

I was wondering if its possible to draw the cells of a ListView with iterating background colors EG:

image

How could I change the cell BG color for odd and even rows ?

This is our sample code: Any suggestions?

AbsoluteLayout cellLayout = new AbsoluteLayout();
ViewCell cell;
ListView listView = new ListView
{
        ItemsSource = sampleItem,
        ItemTemplate = new DataTemplate(() =>
        {
                Label sampleLabel = new Label();
                nameLabel.SetBinding(Label.TextProperty, "Name");
                AbsoluteLayout.SetLayoutBounds(nameLabel, new Rectangle(100f, 18f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
                cellLayout.Children.Add(nameLabel);
                cell = new ViewCell()
                {
                        View = new AbsoluteLayout
                        {
                                Children = { sampleLabel, }
                        }
                };
                //  How could I change the cell BG color for odd and even rows ?
                //cell.View.BackgroundColor = ????
                return cell;
        })
};

Example of a SampleViewModel

List<SampleViewModel> sampleItem = new List< SampleViewModel >
{
        new SampleViewModel("Jon Doe"),
        new SampleViewModel("Will Thompson"),
        new SampleViewModel("Jon Doe"),
};
public SampleViewModel(string name) { this.Name = name; }
public string Name { private set; get; }

Any Help is greatly appreciated.. Thanks in advance...


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>