Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this XAML getting the error: Items collection must be empty before using ItemsSource

Tags:

c#

mvvm

wpf

Can anyone conjure from this code why the ItemsSource line would be getting a

Items collection must be empty before using ItemsSource.

error? Most solutions I've found point to ill-composed XAML, e.g. an extra element etc. which I don't seem to have. When I take out

ItemsSource="{Binding Customers}"

it runs without an error (but of course doesn't display my list of customers).

Customers is defines thusly in the ViewModel and has 3 CustomerViewModels in it:

Customer[] customers = Customer.GetCustomers();
IEnumerable<CustomerViewModel> customersViewModels = customers.Select(c => new CustomerViewModel(c));
this.Customers = new ReadOnlyCollection<CustomerViewModel>(customersViewModels.ToArray());

Any suggestions of where to look?

<UserControl x:Class="TestCommandSink123.View.CustomersView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestCommandSink123"
    xmlns:view="clr-namespace:TestCommandSink123.View"
    xmlns:vm="clr-namespace:TestCommandSink123.ViewModel"
    xmlns:sink="clr-namespace:TestCommandSink123.CommandSinkClasses"
    sink:CommandSinkBinding.CommandSink="{Binding}"
    >

    <UserControl.CommandBindings>
        <sink:CommandSinkBinding Command="vm:CustomersViewModel.CloseAllCustomersCommand"/>
    </UserControl.CommandBindings>

    <DockPanel>
        <ItemsControl
            DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <view:CustomerView/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <Button
                Command="vm:CustomersViewModel.CloseAllCustomersCommand"
                Content="Close All"
                Margin="0,0,0,8"
                />
        </ItemsControl>

    </DockPanel>
</UserControl>

ANSWER:

I did indeed have malformed XAML, just overlooked it, the Button should be outside the ItemsControl:

<ItemsControl
    DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <view:CustomerView/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<Button
    Command="vm:CustomersViewModel.CloseAllCustomersCommand"
    Content="Close All"
    Margin="0,0,0,8"
    />
like image 314
Edward Tanguay Avatar asked Apr 22 '09 11:04

Edward Tanguay


People also ask

Which collection should be empty before using itemssource?

Items collection must be empty before using ItemsSource. My code… Private Sub Window1_Loaded (...) Handles MyBase.Loaded ListViewImages.ItemsSource = From g In db.Graphic _ Order By g.DateAdded Ascending _ Select g End Sub

Why does Silverlight/WPF-items collection must be empty?

Silverlight / WPF - Items collection must be empty before using ItemsSource. Items collection must be empty before using ItemsSource. In general, you are probably getting this message because you are trying to bind to the ItemsSource property and you have hard coded some items in your XAML. You can do one or the other, but not both.

Why can’t I use itemssource with itemssource?

Items collection must be empty before using ItemsSource. This exception occurs when you add items to the ItemsSourcethrough different sources. So Make sure you haven’t accidentally missed a tag, misplaced a tag, added extra tags, or miswrote a tag.

How to use XAML listview with an itemscontrol?

So the XAML initialises the ListView with a single local:ImageView in its Items collection. But when using an ItemsControl you must use either the Items property or the ItemsSource property, you can't use both at the same time.


1 Answers

You are trying to set the ItemsSource of the ItemsControl but you already have children. Which of those two should apply? The Button you put inside the ItemsControl or the collection you are handing into it as ItemsSource? The error message is perfectly reasonable.

You would have to either remove the button from the ItemsControl or remove the ItemsSource attribute. You can't insert items and set ItemsSource at the same time.

like image 131
Joey Avatar answered Nov 10 '22 11:11

Joey