I'm facing an unexpected situation while working with ListView. I've this ListView with rows having a backgroung color according to its parity. It works quite good until I remove an element from the binded collection, and then two rows with the same background color appears together. Here's my code:
<ResourceDictionary>
<DataTemplate x:Key="EvenIndexDataTemplate">
<ViewCell>
<Grid BackgroundColor="LightGray"">
<!-- my content here -->
</Grid>
</ViewCell>
</DataTemplate>
<DataTemplate x:Key="OddIndexDataTemplate">
<ViewCell>
<Grid BackgroundColor="White"">
<!-- my content here -->
</Grid>
</ViewCell>
</DataTemplate>
<datatemp:EvenOddTemplateSelector x:Key="MyDataTemplate"
EvenTemplate="{StaticResource EvenIndexDataTemplate}"
OddTemplate="{StaticResource OddIndexDataTemplate}" />
</ResourceDictionary>
<ContentPage.Content>
<!-- something here -->
<ListView ItemsSource="{Binding ItemsSource}"
ItemTemplate="{StaticResource ItemTemplateResource}" />
<!-- something else here -->
</ContentPage.Content>
My DataTemplateSelector class:
public class EvenOddTemplateSelector : DataTemplateSelector
{
public DataTemplate EvenTemplate { get; set; }
public DataTemplate OddTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var itemsView = (ListView)container;
return ((IList)itemsView.ItemsSource).IndexOf(item) % 2 == 0 ? EvenTemplate : OddTemplate;
}
What I want to get is to keep adjacents rows with different background color after removing an item. At this point ItemsSource gets modified only by Adding/Removing. I've not tried to insert an item. I don't need it for this feature I'm working on.
Any idea?
I just solved the issue. Here's what I did:
I created a customization of
ViewCellclass and added three bindable propertiesContainerItemsSource,EvenBackgroundColorandOddBackgroundColor, then I overrideOnBindingContextChanged()to make sure to setViewbackground color by its index within the collection.You can check it out on this new brach on github repo.
Basically the idea is to use it on this way:
My custom
EvenOddCell: