title: “Items collection must be empty before using ItemsSource.” error wpf
body: <p>I am trying to bind an observable collection of strings. But when i launch an app, I receive Exception that Items collection must be empty before using ItemsSource. I have no elements in collection when it is binding, so what can be the issue? </p>
  
  <p>My Xaml</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Users}"  Margin="10,77,805,228" Grid.RowSpan="2"&gt;
                      &lt;ListBoxItem&gt;
                          &lt;DataTemplate&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
  
                              &lt;/StackPanel&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListBoxItem&gt;
                  &lt;/ListBox&gt;
  &lt;Button x:Name="AddUserButton" Content="Додати" Command="{Binding AddUserCommand}" RenderTransformOrigin="0.512,1.9"  /&gt;
  </code></pre>
  
  <p>My ViewModel (command and observablecollection)</p>
  
  <pre><code>public class UsersTabViewModel : ViewModelBase
  {
      private ObservableCollection&lt;string&gt; users;
      private string text;
  
      private ICommand addUserCommand;
  
      private bool _canExecute;
  
      public UsersTabViewModel() 
      {
          _canExecute = true;
          Users = new ObservableCollection&lt;string&gt;();
      }
  
      public ObservableCollection&lt;string&gt; Users { get; set; }
  
  
      public ICommand AddUserCommand
      {
          get
          {
              return addUserCommand ?? (addUserCommand = new CommandHandler(() =&gt; AddUserAction(), _canExecute)); 
          }
  
      }
  
      public string Text
      {
          get
          {
              return text;
          }
  
          set
          {
              text = value;
          }
      } 
  
      //text is bound to here
      private void AddUserAction()
      {
  
          Users.Add("collection");
  
  
      }
  
      public class CommandHandler : ICommand
      {
          private Action _action;
          private bool _canExecute;
          public CommandHandler(Action action, bool canExecute)
          {
              _action = action;
              _canExecute = canExecute;
          }
  
          public bool CanExecute(object parameter)
          {
              return _canExecute;
          }
  
          public event EventHandler CanExecuteChanged;
  
          public void Execute(object parameter)
          {
              _action();
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  }
  </code></pre>