title: WPF ListView: Changing ItemsSource does not change ListView
body: <p>I am using a <code>ListView</code> control to display some lines of data. There is a background task which receives external updates to the content of the list. The newly received data may contain less, more or the same number of items and also the items itself may have changed.</p>
  
  <p>The <code>ListView.ItemsSource</code> is bound to an <code>OberservableCollection</code> (_itemList) so that changes to _itemList should be visible also in the <code>ListView</code>.</p>
  
  <pre><code>_itemList = new ObservableCollection&lt;PmemCombItem&gt;();
  _itemList.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
  L_PmemCombList.ItemsSource = _itemList;
  </code></pre>
  
  <p>In order to avoid refreshing the complete ListView I do a simple comparison of the newly retrieved list with the current _itemList, change items which are not the same and add/remove items if necessary. The collection "newList" contains newly created objects, so replacing an item in _itemList is correctly sending a "Refresh" notification (which I can log by using the event handler <code>OnCollectionChanged</code> of the ObservableCollection`)</p>
  
  <pre><code>Action action = () =&gt;
  {
      for (int i = 0; i &lt; newList.Count; i++)
      {
          // item exists in old list -&gt; replace if changed
          if (i &lt; _itemList.Count)
          {
              if (!_itemList[i].SameDataAs(newList[i]))
                  _itemList[i] = newList[i];
          }
          // new list contains more items -&gt; add items
          else
              _itemList.Add(newList[i]);
       }
       // new list contains less items -&gt; remove items
       for (int i = _itemList.Count - 1; i &gt;= newList.Count; i--)
           _itemList.RemoveAt(i);
   };
   Dispatcher.BeginInvoke(DispatcherPriority.Background, action);
  </code></pre>
  
  <p>My problem is that if many items are changed in this loop, the <code>ListView</code> is NOT refreshing and the data on screen stay as they are...and this I don't understand.</p>
  
  <p>Even a simpler version like this (exchanging ALL elements)</p>
  
  <pre><code>List&lt;PmemCombItem&gt; newList = new List&lt;PmemCombItem&gt;();
  foreach (PmemViewItem comb in combList)
      newList.Add(new PmemCombItem(comb));
  
  if (_itemList.Count == newList.Count)
      for (int i = 0; i &lt; newList.Count; i++)
          _itemList[i] = newList[i];
  else
  {
      _itemList.Clear();
      foreach (PmemCombItem item in newList)
          _itemList.Add(item);
  }
  </code></pre>
  
  <p>is not working properly</p>
  
  <p>Any clue on this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I call the following code manually after updating all elements, everything works fine</p>
  
  <pre><code>OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  </code></pre>
  
  <p>But of course this causes the UI to update everything which I still want to avoid.</p>