I have noticed that when I try to employ a DataTemplateSelector on both the ItemTemplate and the GroupHeaderTemplate (a different data template selector for each) in a ListView, that--at least in Android--this tends to cause an ArgumentOutOfRangeException. I'm not exactly sure why this happens but it is certainly unfortunate for what I'm trying to do. I have used DataTemplateSelectors before plenty of times for ItemTemplates and GroupHeaderTemplates, and they have worked fine, but this is the first time I have used them at the same time. While stepping through the code I have noticed two things:
- The OnSelectTemplate method for both of the DataTemplateSelectors are always called on every item, regardless of if it is a group header item or just a list item. This causes a faulty template to be returned by the group header DataTemplateSelector when a list item is being rendered (while the list item one returns the correct template) and vice versa.
- The Listview raises the exception on scroll. So when more items from the observable collection are being drawn onto the screen it somehow runs into an ArgumentOutOfRangeException.
If anyone has encountered this and has a solution or some work around it would be greatly appreciated. For the time being I'm going to try and put everything into one DataTemplateSelector that gets called for both the items and the group headers. Hopefully that will at least mitigate the faulty templates being returned.
EDIT Consolidating this into one datatemplateselector didn't work, but I guess it was worth the try.