views:

1361

answers:

3

Hello,

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

+1  A: 

First, remove ItemsSource="{Binding}" from your ItemsControl. This should fix your error i believe.

Secondly, I'm not sure if your WrapPanel is going to work as expected in this case. From my understanding, WrapPanel will do wrapping when it has multiple children that extend out of bounds. In this case, your WrapPanel only has 1 child, an ItemsControl.

Will Eddins
+1  A: 

Apparently you're using the MVVM pattern. In that case you shouldn't explicitly assign a collection to the ItemsSource property... instead, you should assign a ViewModel to the DataContext of the Window (or UserControl). If your DataContext is _mainViewModel, your binding should be :

<ItemsControl ItemsSource="{Binding CustomerCollection}" ...
Thomas Levesque
Thanks for the hint, I'll correct this.
Joseph Melettukunnel
+1  A: 

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.

Joe White
Great.. it's working now!Thanks a lot, sometimes I wish I could kick myself :-)
Joseph Melettukunnel