title: Binding observable collection to ListBox in XAML
body: <p>I've spent lots of hours with this problem.</p>
  
  <p>I have a class with data: </p>
  
  <pre><code>public class User : INotifyPropertyChanged
  {
      private int _key;
      private string _fullName;
      private string _nick;
  
      public int Key
      {
          get{return _key;}
          set { _key = value; NotifyPropertyChanged("Key"); }
      }
      public string Nick
      {
          get { return _nick; }
          set { _nick = value; NotifyPropertyChanged("Nick"); }
      }
      public string FullName
      {
          get { return _fullName; }
          set { _fullName = value; NotifyPropertyChanged("FullName"); }
      }
  
  
      public User()
      {
          Nick = "nickname";
          FullName = "fullname";
      }
  
      public User(String nick, String name, int key)
      {
          Nick = nick;
          FullName  = name;
      }
  
  
      //INotifyPropertyChanged implementation
      public event PropertyChangedEventHandler PropertyChanged;
      private void NotifyPropertyChanged(String propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      public override string ToString() 
      { 
          return string.Format("{0} {1}, {2}", Key, Nick, FullName); 
      }
  
  }
  </code></pre>
  
  <p>Next I have a class with observablecollection of userClass class:</p>
  
  <pre><code>public class UserList : ObservableCollection&lt;UserList&gt;
  {
      public UserList (){}
  
      ~UserList ()
      {
          //Serialize();
      }
  
      public void Serialize(ObservableCollection&lt;UserList&gt; usersColl) 
      {
          FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
          BinaryFormatter formatter = new BinaryFormatter();
          try
          {
              formatter.Serialize(fs, usersColl);
          }
          catch (SerializationException e)
          {
              Console.WriteLine("Failed to serialize. Reason: " + e.Message);
              throw;
          }
          finally
          {
              fs.Close();
          }
      }
  
      public void Deserialize() 
      {
          FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
          try 
          {
              BinaryFormatter formatter = new BinaryFormatter();
              //users = (Hashtable) formatter.Deserialize(fs);
              //usersColl = (ObservableCollection&lt;userClass&gt;)formatter.Deserialize(fs);
          }
          catch (SerializationException e) 
          {
              MessageBox.Show(" Error: " + e.Message);
              throw;
          }
          finally 
          {
              fs.Close();
          }
      }
  
  }
  </code></pre>
  
  <p>In fact, after lots of testing an editing, big part of code doesn't work, like serialization. But it is not necessary for data binding and binding is what i am solving now.</p>
  
  <p>So i have this collection and want to bind it to listBox.
  I tried several ways, but haven't got it to work. </p>
  
  <p>The last one I tried gave me the write error: </p>
  
  <blockquote>
    <p><em>The resource 'users' cannot be resolved.</em></p>
  </blockquote>
  
  <pre><code>&lt;ListBox Grid.Column="0" Name="userViewLeft" ItemsSource="{Binding Source={StaticResource users} }" /&gt;
  </code></pre>