notes of an interactive developer

The Elusive ArgumentException in a List Control…

The last week or so I have been struggling with a bug that was keeping me away from updating WinRT XAML Toolkit. After adding an ItemTemplateSelector for my custom ListBox – I started getting ArgumentExceptions with a vague “Value does not fall within the expected range.” message and of course none of my code on the stack. At first I thought my custom ListBox subclass was causing some problems that the platform couldn’t handle, but then after replacing it with a regular ListBox it kept happening. Finally I noticed that replacing the DataTemplate I was using with another one that was simpler stopped the exception. Then I modified that template to look the same as the one I wanted to use – and it still worked fine! It took me probably 2 minutes of playing spot the difference to notice that the bad template had a DataTemplate inside of the DataTemplate! That obviously doesn’t generate a good list item! Hopefully this post helps someone working with a ListBox, ListView or GridView and getting these ArgumentExceptions that crash the app with no details of where they come from…