GroupHeaderTemplate and ItemTemplate DataTemplateSelector conflict?

JonathanCook.8900JonathanCook.8900 USMember ✭✭
edited November 2018 in Xamarin.Forms

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:

  1. 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.
  2. 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.


  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo?

  • So in the process of trying to whip up an example of this error I found out what the issue is. Though I'm still not exactly sure why it only happens when GroupHeader DataTemplateSelectors are used. The exception is raised when I create a grouping that has no children (which is a requirement for what I am working on). Normally this would just create a groupheader that has no items under it. But when the group header data template selector is used it raises an ArgumentOutOfRangeException when it renders groupings without children. If you would like to experience this first hand I will attach a link to an example project. Note that the line that causes the project to crash is commented out in MainPageVM.

  • JohnHardmanJohnHardman GBUniversity mod

    @JonathanCook.8900 - Do you know if this has been fixed, and if so, in which version of XF? Many thanks

Sign In or Register to comment.