title: Bind WPF DataGrid ItemsSource to method, IEnumerable<TableRow> GetRows(), of custom Table object?
body: <p>I am struggling with the learning curve on WPF data binding and could use an example.  Bonus points for those that answer the question and link to an article that helped them "get" WPF data binding.</p>
  
  <p>I am trying to bind a custom Table object with a WPF DataGrid.</p>
  
  <p>Here is my objects (I do not have the ability to change them, signatures truncated a bit):</p>
  
  <pre><code>public class MyTable
  {
      public int ColumnCount { get; }
      public string GetColumnName(int columnIndex);
      public IEnumerable&lt;MyTableRow&gt; GetRows();
  }
  
  public class MyTableRow
  {
      public MyTableCell [] Cells { get; }
  }
  
  public class MyTableCell
  {
      public string Value { get; }
  }
  </code></pre>
  
  <p>So the <code>DataContext</code> on the current <code>Window</code> is an instance of <code>MyTable</code>. I could not see any clear way to auto generate the columns from my <code>MyTable</code> object so I generated them in code:</p>
  
  <pre><code>private void dg_Table_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  {
      MyTable tbl = e.NewValue as MyTable;
  
      if (tbl != null)
      {
          //setup data grid columns
          dg_Table.Columns.Clear();
  
          for (int i = 0; i &lt; tbl.ColumnCount; i++)
          {
              var column = new DataGridTextColumn()
              {
                  Header = tbl.GetColumnName(i),
                  Binding = new Binding(string.Format("Cells[{0}]", i))
              };
  
              dg_Table.Columns.Add(column);
          }
          //end setup data grid columns
      }
  }
  </code></pre>
  
  <p>I think the next step would be to bind the <code>ItemsSource</code> property of the <code>DataGrid</code> to the <code>GetRows</code> method on the <code>MyTable</code> object but I'm not sure how to do that.  I thought maybe this could be done using an <code>ObjectDataProvider</code> resource but I can't figure out how to reference a method on the DataContext object.</p>
  
  <p>Can someone help me with the XAML and code behind for this scenario?</p>