title: WPF DataGrid is adding extra "ghost" row
body: <p>Hei,</p>
  
  <p>In my application i'm using DataGrid to show some data. To get everything working with threading i'm using <a href="http://www.thomaslevesque.com/2009/04/17/wpf-binding-to-an-asynchronous-collection/" rel="noreferrer"><code>AsyncObservableCollection</code></a> as DataContext of DataGrid. When my application starts it looks for files in some folders and updates <code>AsyncObservableCollection</code>. Finding files is done on a separate thread:</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt; _cardType.InitAllOrdersCollection())
      .ContinueWith((t) =&gt; ThrowEvent(), TaskContinuationOptions.None);
  </code></pre>
  
  <p>Where all the loading logic is in <code>InitAllOrdersCollection()</code> method.</p>
  
  <p>Now here's where things go bad, when i start the application for some reason i get 2 rows with same data in DataGrid even if there is one item in collection and only one file in folders. If i add a delay(<code>Thread.Sleep()</code> 50ms minimum) before loading files then DataGrid show everything correctly (no extra row). Delay has to be added to the Thread what is loading the files (The one created with <code>Task.Factory.StartNew()</code>).</p>
  
  <p>Have anybody encountered something similar or is there something else i should try? 
  Thanks in advance!</p>
  
  <p>EDIT: Adding some code as requested:</p>
  
  <pre><code>public AsyncObservableCollection&lt;IGridItem&gt; OrdersCollection = new AsyncObservableCollection&lt;IGridItem&gt;();
  
  public void InitAllOrdersCollection()
  {
      // Thread.Sleep(50); &lt;-- this sleep here fixes the problem!
      foreach (var convention in FileNameConventions)
      {
          var namePatterns = convention.NameConvention.Split(',');
          foreach (var pattern in namePatterns)
          {
              var validFiles = CardTypeExtensions.GetFiles(this.InputFolder, pattern, convention);
              if (validFiles.Any())
              {
                  this.FilesToOrders(validFiles, convention);
              }
          }
      }
  }
  
  public static List&lt;string&gt; GetFiles(string inputFolder, string pattern, FileNameConvention convention)
  {
      var files = Directory.GetFiles(inputFolder, pattern);
      return files.Where(file =&gt; IsCorrect(file, convention)).AsParallel().ToList();
  }
  
  // Adds new order to OrdersCollection if its not there already!
  private void FilesToOrders(List&lt;string&gt; dirFiles, FileNameConvention convention)
  {
      foreach (var dirFile in dirFiles.AsParallel())
      {
          var order = new Order(dirFile, this, convention);
  
          if (!this.OrdersCollection.ContainsOrder(order))
          {
                  this.OrdersCollection.Add(order);
          }
      }
  }
  
  public static bool ContainsOrder(this ObservableCollection&lt;IGridItem&gt; collection, Order order)
  {
      return collection.Cast&lt;Order&gt;().Any(c=&gt;c.Filepath == order.Filepath);
  }
  </code></pre>
  
  <p><code>FilesToOrders()</code> method is the one what adds the new orders to the <code>AsyncObservableCollection</code>.
  Hope this helps.</p>