title: How do I programmatically bind a WPF Datagrid to a ObservableCollection of custom objects?
body: <p>I've been searching for ages and haven't found any solution. Title basically covers what I'm trying to accomplish. Here's what I have so far that clearly isn't working with irrelevant stuff cut out.</p>
  
  <p>In MainWindow.xaml.cs:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
  
      ModGrid = new DataGrid();
      ModGrid.CanUserReorderColumns = false;
  
      DataGridTextColumn Name = new DataGridTextColumn();
      Name.Header = "Name";
      Name.MinWidth = 45;
      Name.Width = (DataGridLength)(new DataGridLengthConverter()).ConvertFromString("*");
      Binding nameBinding = new Binding("Name");
      nameBinding.Mode = BindingMode.OneWay;
      Name.Binding = nameBinding;
      ModGrid.Columns.Add(Name);
  
      DataGridTextColumn Tags = new DataGridTextColumn();
      Tags.Header = "Tags";
      Tags.MinWidth = 40;
      Tags.Width = 200;
      Binding tagsBinding = new Binding("Tags");
      tagsBinding.Mode = BindingMode.OneWay;
      Tags.Binding = tagsBinding;
      ModGrid.Columns.Add(Tags);
  
      // There are more columns but you get the idea
  
      ModGrid.ItemsSource = Database.Mods;
  }
  
  private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
  {
      ModGrid.DataContext = Database.Mods;
  }
  </code></pre>
  
  <p>Later I add items to Database.Mods</p>
  
  <pre><code>void Fetcher_RequestComplete(bool error, string result, int requestsLeft)
  {
      if (!error)
      {
          Database.Mods = Parser.ParseMods(result);
      }
  }
  </code></pre>
  
  <p>This is all that's in database.cs</p>
  
  <pre><code>public static class Database
  {
      public static ObservableCollection&lt;Mod&gt; Mods = new ObservableCollection&lt;Mod&gt;();
  }
  </code></pre>
  
  <p>And this is all that's in mod.cs</p>
  
  <pre><code>public class Mod
  {
      public Mod() { }
  
      public Image Status;
      public string Name;
      public string Description;
      public string Author;
      public string Tags;
      public int Views;
      public string Link;
      public string[] Versions;
      public string[] TagsList;
  }
  </code></pre>
  
  <p>Sorry about the code spam but I haven't seen how to do this entirely programmatically and it's necessary that I do. Any responses at all are <em>very</em> appreciated!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>While I did find an answer, if your in this situation I've found it would be much better to just make a custom control and have access from the XAML that way.</p>