Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. int position = 1;
  2. foreach (var i in yourItemList)
  3. {
  4. youranotherProperty = "";
  5. ItemBackgroundColor = (position % 2 == 0)? "Green" : "Red" ;
  6. position++;
  7. }
  8.  
  9. public class AlternateColorDataTemplateSelector : DataTemplateSelector
  10. {
  11. public DataTemplate EvenTemplate { get; set; }
  12. public DataTemplate UnevenTemplate { get; set; }
  13.  
  14. protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
  15. {
  16. // TODO: Maybe some more error handling here
  17. return ((List<string>)((ListView)container).ItemsSource).IndexOf(item as string) % 2 == 0 ? EvenTemplate : UnevenTemplate;
  18. }
  19. }
  20.  
  21. <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">
  22. <ContentPage.Resources>
  23. <ResourceDictionary>
  24. <DataTemplate x:Key="evenTemplate">
  25. <ViewCell>
  26. <Grid BackgroundColor="White">
  27. <Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center" />
  28. </Grid>
  29. </ViewCell>
  30. </DataTemplate>
  31. <DataTemplate x:Key="unevenTemplate">
  32. <ViewCell>
  33. <Grid BackgroundColor="LightGray">
  34. <Label Text="{Binding .}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
  35. </Grid>
  36. </ViewCell>
  37. </DataTemplate>
  38. <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
  39. EvenTemplate="{StaticResource evenTemplate}"
  40. UnevenTemplate="{StaticResource unevenTemplate}" />
  41. </ResourceDictionary>
  42. </ContentPage.Resources>
  43.  
  44. <ListView ItemTemplate="{StaticResource alternateColorDataTemplateSelector}" ItemsSource="{Binding Items}">
  45.  
  46. </ListView>
  47. </ContentPage>
Add Comment
Please, Sign In to add comment