One issue I came across while using a ListBox with a DataTemplate, instead of a DataGrid was a compiler/xaml error stating that the Items collection should be empty…

This was a little baffling and I was wondering if there were data-binding problems in the code-behind, etc. Turns out the source of the problem was in the XAML.

I had:

<ListBox x:Name=”foo”>
     <DataTemplate>

and I needed to add in a ItemTemplate container tag, like so:

<ListBox x:Name=”foo”>
     <ListBox.ItemTemplate>
          <DataTemplate>

I think the error was happening because the framework was considering the DataTemplate as an item in the list, and hence the Items collection was not empty and messed with the binding.
Adding the ItemTemplate tag specified it as a template, and not a list-item

Advertisements