title: Merged ObservableCollection
body: <p>I have two ObservableCollections and I need to show them in one ListView control together. For this purpose I created MergedCollection which presents these two collections as one ObservableCollection. This way I can set the ListView.ItemsSource to my merged collection and both collections are listed. Adding works fine but when I try to Remove an item, unhandled exception is shown:</p>
  
  <pre><code>An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
  Additional information: Added item does not appear at given index '2'.
  </code></pre>
  
  <p>The code of MergedCollection follows:</p>
  
  <pre><code>public class MergedCollection : IEnumerable, INotifyCollectionChanged
  {
      ObservableCollection&lt;NetworkNode&gt; nodes;
      ObservableCollection&lt;NodeConnection&gt; connections;
  
      public MergedCollection(ObservableCollection&lt;NetworkNode&gt; nodes, ObservableCollection&lt;NodeConnection&gt; connections)
      {
          this.nodes = nodes;
          this.connections = connections;
  
          this.nodes.CollectionChanged += new NotifyCollectionChangedEventHandler(NetworkNodes_CollectionChanged);
          this.connections.CollectionChanged += new NotifyCollectionChangedEventHandler(Connections_CollectionChanged);
      }
  
      void NetworkNodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
          CollectionChanged(this, e);
      }
  
      void Connections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
          CollectionChanged(this, e);
      }
  
      #region IEnumerable Members
  
      public IEnumerator GetEnumerator()
      {
          for (int i = 0; i &lt; connections.Count; i++)
          {
              yield return connections[i];
          }
  
          for (int i = 0; i &lt; nodes.Count; i++)
          {
              yield return nodes[i];
          }
      }
  
      #endregion
  
      #region INotifyCollectionChanged Members
  
      public event NotifyCollectionChangedEventHandler CollectionChanged;
  
      #endregion
  }
  </code></pre>
  
  <p>Regards</p>