title: Why is this XAML getting the error: Items collection must be empty before using ItemsSource
body: <p>Can anyone conjure from this code why the ItemsSource line would be getting a </p>
  
  <blockquote>
    <p><strong>Items collection must be empty before</strong>
    <strong>using ItemsSource.</strong></p>
  </blockquote>
  
  <p>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</p>
  
  <blockquote>
    <p>ItemsSource="{Binding Customers}"</p>
  </blockquote>
  
  <p>it runs without an error (but of course doesn't display my list of customers).</p>
  
  <p>Customers is defines thusly in the ViewModel and has 3 CustomerViewModels in it:</p>
  
  <pre><code>Customer[] customers = Customer.GetCustomers();
  IEnumerable&lt;CustomerViewModel&gt; customersViewModels = customers.Select(c =&gt; new CustomerViewModel(c));
  this.Customers = new ReadOnlyCollection&lt;CustomerViewModel&gt;(customersViewModels.ToArray());
  </code></pre>
  
  <p><strong>Any suggestions of where to look?</strong></p>
  
  <pre><code>&lt;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}"
      &gt;
  
      &lt;UserControl.CommandBindings&gt;
          &lt;sink:CommandSinkBinding Command="vm:CustomersViewModel.CloseAllCustomersCommand"/&gt;
      &lt;/UserControl.CommandBindings&gt;
  
      &lt;DockPanel&gt;
          &lt;ItemsControl
              DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}"&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;view:CustomerView/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
              &lt;Button
                  Command="vm:CustomersViewModel.CloseAllCustomersCommand"
                  Content="Close All"
                  Margin="0,0,0,8"
                  /&gt;
          &lt;/ItemsControl&gt;
  
      &lt;/DockPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <h1>ANSWER:</h1>
  
  <p>I did indeed have malformed XAML, just overlooked it, <strong>the Button should be outside the ItemsControl</strong>:</p>
  
  <pre><code>&lt;ItemsControl
      DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;view:CustomerView/&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  &lt;Button
      Command="vm:CustomersViewModel.CloseAllCustomersCommand"
      Content="Close All"
      Margin="0,0,0,8"
      /&gt;
  </code></pre>