Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int position = 1;
- foreach (var i in yourItemList)
- {
- youranotherProperty = "";
- ItemBackgroundColor = (position % 2 == 0)? "Green" : "Red" ;
- position++;
- }
- public class AlternateColorDataTemplateSelector : DataTemplateSelector
- {
- public DataTemplate EvenTemplate { get; set; }
- public DataTemplate UnevenTemplate { get; set; }
- protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
- {
- // TODO: Maybe some more error handling here
- return ((List<string>)((ListView)container).ItemsSource).IndexOf(item as string) % 2 == 0 ? EvenTemplate : UnevenTemplate;
- }
- }
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AlternateRowColorSample" x:Class="AlternateRowColorSample.MainPage">
- <ContentPage.Resources>
- <ResourceDictionary>
- <DataTemplate x:Key="evenTemplate">
- <ViewCell>
- <Grid BackgroundColor="White">
- <Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center" />
- </Grid>
- </ViewCell>
- </DataTemplate>
- <DataTemplate x:Key="unevenTemplate">
- <ViewCell>
- <Grid BackgroundColor="LightGray">
- <Label Text="{Binding .}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
- </Grid>
- </ViewCell>
- </DataTemplate>
- <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
- EvenTemplate="{StaticResource evenTemplate}"
- UnevenTemplate="{StaticResource unevenTemplate}" />
- </ResourceDictionary>
- </ContentPage.Resources>
- <ListView ItemTemplate="{StaticResource alternateColorDataTemplateSelector}" ItemsSource="{Binding Items}">
- </ListView>
- </ContentPage>
Add Comment
Please, Sign In to add comment