WPF Error: “Items collection must be empty before using ItemsSource.”

itemscontrollistwpf

Does anyone know why I keep getting the "Items collection must be empty before using ItemsSource"-Error?

Here is the code:

        <ScrollViewer Margin="8,8,8,8" Grid.Row="3" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">                  
                <WrapPanel Orientation="Vertical">
                    <ItemsControl ItemsSource="{Binding}" x:Name="CustomerList" >>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Horizontal">
                                </WrapPanel>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <View:UserControlCustomerDetails>
                                </View:UserControlCustomerDetails>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </WrapPanel>
            </ScrollViewer>

This is what I do in my Code-Behind:

 CustomerList.ItemsSource = _mainViewModel.CustomerCollection;

Note that CustomerCollection is just a simple List<Customers>.

Thanks for your help!

Cheers

Best Solution

Is this code copied verbatim? Do you really have two right angle brackets (>>) at the end of the <ItemsControl... line? If so, the second right angle bracket might be getting treated as text content, which is getting added to the Items collection.

Related Question