title: Implementing INotifyCollectionChanged on a collection without indexes
body: <p>I am just now getting my toes wet in WPF after several years of working in ASP.Net exclusively.  The problem I am currently struggling with is that I have a custom collection class which I need to bind to a listbox.  Everything seems to be working except for removing an item from the collection.  When I try to I get the error: <code>“Collection Remove event must specify item position.”</code>  The catch is that this collection does not use indexes so I am not seeing a way to specify the position and so far Google has failed to show me a workable solution…</p>
  
  <p>The class is defined to implement <code>ICollection&lt;&gt;</code> and <code>INotifyCollectionChanged</code>.  My internal items container is a <code>Dictionary</code> which uses the item’s Name(string) value for a key.  Aside from the methods defined by these two interfaces, this collection has an indexer that allows items to be accessed by Name, and overrides for the <code>Contains</code> and <code>Remove</code> methods so that they can also be called with the item Name.  This is working for Adds and Edits but throws the above exception when I try to remove.</p>
  
  <p>Here is an excerpt of the relevant code:</p>
  
  <pre><code>class Foo
  {
      public string Name
      {
          get;
          set;
      }
  }
  class FooCollection : ICollection&lt;Foo&gt;, INotifyCollectionChanged
  {
      Dictionary&lt;string, Foo&gt; Items;
  
      public FooCollection()
      {
          Items = new Dictionary&lt;string, Foo&gt;();
      }
  
      #region ICollection&lt;Foo&gt; Members
  
      //***REMOVED FOR BREVITY***
  
      public bool Remove(Foo item)
      {
          return this.Remove(item.Name);
      }
      public bool Remove(string name)
      {
          bool Value = this.Contains(name);
          if (Value)
          {
              NotifyCollectionChangedEventArgs E = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, Items[name]);
              Value = Items.Remove(name);
              if (Value)
              {
                  RaiseCollectionChanged(E);
              }
          }
          return Value;
      }
      #endregion
  
      #region INotifyCollectionChanged Members
      public event NotifyCollectionChangedEventHandler CollectionChanged;
      private void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
      {
          if (CollectionChanged != null)
          {
              CollectionChanged(this, e);
          }
      }
      #endregion
  }
  </code></pre>