Question

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

Was it helpful?

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.

OTHER TIPS

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.

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}" ...

Use DataGrid.Items.Clear(); I hope it will be Helpfull...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top