“value does not fall within the expected range” XAML exception using DataTemplateSelector

For the record I made a stupid error in Xaml and got bashed with the usual unhelpful error message (no stack trace, no line numbers just the message you see above).

I’d created a custom DataTemplateSelector but rather than set the ItemTemplateSelector property on the GridView, I’d put it inside the . Nothing complained but the app threw a runtime exception as above.

Instead putting the DataTemplateSelector as a static resource and setting ItemTemplateSelector=”{StaticResource myTemplateSelector}” resolved the problem.