title: How to bind a collection to a ListView in WPF
body: <p>I have a program that searches a directory for files matching certain criteria. This search process takes a <strong>long</strong> time, so I have to call it asynchronously. When the search algorithm finds a file, it triggers an event. My <code>MainWindow</code> instance listens for this event and needs to update the GUI. How can I bind these "added" files to a <code>ListView</code>? I figured that I could use an <code>ObservableCollection&lt;FileInfo&gt;</code> instance, but I can't figure out how to bind it.</p>
  
  <p>I've stripped out all of the irrelevant controls and code. Here are the two relevant files.</p>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="Example.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="CR Search" Height="395" Width="525"&gt;
      &lt;Grid&gt;
          &lt;ListView x:Name="Results"&gt;
              &lt;ListView.View&gt;
                  &lt;GridView&gt;
                      &lt;GridViewColumn Header="Filename"/&gt;
                      &lt;GridViewColumn Header="Directory"/&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre><code>using System.IO;
  using System.Threading.Tasks;
  
  public partial class MainWindow
  {
      private SearchLogic _backgroundSearch;
  
      private async void Search(object sender, RoutedEventArgs e)
      {
          // TODO: clear Results
  
          _backgroundSearch = new SearchLogic("", new DirectoryInfo("C:\"));
          _backgroundSearch.FileAdded += FileAdded;
  
          await Task.Run(new Action(_backgroundSearch.Search));
      }
  
      private void FileAdded(object sender, FileAddedEventArgs eventArgs)
      {
          // TODO: add eventArgs.File to Results
          // eventArgs.File is an instance of FileInfo
      }
  }
  </code></pre>