title: Listbox in WPF: Items collection must be empty before using ItemsSource
body: <p>I'm having some issues with the ItemsSource property in WPF (for binding). I can't run my program while having the "ItemsSource="{Binding}" as a property for my ListBox. Although, if I remove it, the program runs fine but the binding doesn't work.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="TP1.EcranPrincipal"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:TP1"
          mc:Ignorable="d"
          Background="CornflowerBlue"
          Title="" Height="400" Width="700"&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="masterTemplate"&gt;
              &lt;TextBlock
            Margin="4"
            Text="{Binding Description,UpdateSourceTrigger=PropertyChanged}"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="2*"/&gt;
              &lt;ColumnDefinition Width="*"/&gt;
              &lt;ColumnDefinition Width="2*"/&gt;
              &lt;ColumnDefinition Width="*"/&gt;
              &lt;ColumnDefinition Width="2*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;TextBox Grid.Column="1" Grid.Row="0" Padding="5" VerticalContentAlignment="Center" Margin="10,10,90,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Grid.ColumnSpan="4"/&gt;
          &lt;Button x:Name="BoutonRechercher" Grid.Column="4" Grid.Row="0" Padding="5" Content="Rechercher" HorizontalAlignment="Left" Margin="88,9,0,0" VerticalAlignment="Top" Click="BoutonRechercher_Click" Width="75"/&gt;
  
          &lt;!-- 1st TextBoxes' column --&gt;
          &lt;TextBox x:Name="Nom" Text="{Binding Nom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="1" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Telephone" Text="{Binding Téléphone,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="2" Margin="0, 0, 10, 10"  VerticalContentAlignment="Center" Padding="5,0" /&gt;
          &lt;TextBox x:Name="Adresse1" Text="{Binding Adresse1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="3" Margin="0, 0, 10, 10" Grid.ColumnSpan="3" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Adresse2" Text="{Binding Adresse2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="4" Margin="0, 0, 10, 10" Grid.ColumnSpan="3" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Ville" Text="{Binding Ville,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="5" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Pays" Text="{Binding Pays,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="6" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
  
          &lt;!-- 2nd TextBoxes' column --&gt;
          &lt;TextBox x:Name="Prenom" Text="{Binding Prénom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="1" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Courriel" Text="{Binding Courriel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="2" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="Province" Text="{Binding Province,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="5" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
          &lt;TextBox x:Name="CodePostal" Text="{Binding CodePostal,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="6" Margin="0, 0, 10, 10" VerticalContentAlignment="Center"/&gt;
  
          &lt;ListBox x:Name="ListeBoxUtilisateurs" ItemsSource="{Binding}" ItemTemplate="{StaticResource masterTemplate}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Height="349" Margin="10,10,0,0" VerticalAlignment="Top" Width="153" Grid.RowSpan="7"/&gt;
  
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  // StreamReader/Writer:
  using System.IO;
  using System.ComponentModel;
  
  namespace TP1
  {
      /// &lt;summary&gt;
      /// Logique d'interaction pour EcranPrincipal.xaml
      /// &lt;/summary&gt;
      public partial class EcranPrincipal : Window
      {
  
          // Required variables to read the file
          List&lt;String&gt; elementsLignes = new List&lt;String&gt;();
          List&lt;Utilisateur&gt; utilisateurs = new List&lt;Utilisateur&gt;();
          Collection Coll = new Collection();
  
  
  
          public EcranPrincipal()
          {
              InitializeComponent();
              this.DataContext = Coll;
              ListBox listeUtilisateurs = ListeBoxUtilisateurs;
  
              var reader = new StreamReader(File.OpenRead("TextFile.txt"));
              int nbUtilisateurs = 0;
  
              Console.WriteLine(reader.ReadLine());
  
              while (reader.ReadLine() != null)
              {
                 // Reading the file, this works fine
              }
          }
  
  
          private ListBoxItem selectedItem;
          public ListBoxItem SelectedItem
          {
              get { return selectedItem; }
              set { selectedItem = value; }
          }
      }
  }
  </code></pre>
  
  <p>Everytime I run my program, I get this <em>Items collection must be empty before using ItemsSource</em> error. Anyone figured out what my problem is?</p>