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.
I am trying to bind a custom Table object with a WPF DataGrid.
Here is my objects (I do not have the ability to change them, signatures truncated a bit):
public class MyTable
{
public int ColumnCount { get; }
public string GetColumnName(int columnIndex);
public IEnumerable<MyTableRow> GetRows();
}
public class MyTableRow
{
public MyTableCell [] Cells { get; }
}
public class MyTableCell
{
public string Value { get; }
}
So the DataContext on the current Window is an instance of MyTable. I could not see any clear way to auto generate the columns from my MyTable object so I generated them in 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 < 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
}
}
I think the next step would be to bind the ItemsSource property of the DataGrid to the GetRows method on the MyTable object but I'm not sure how to do that. I thought maybe this could be done using an ObjectDataProvider resource but I can't figure out how to reference a method on the DataContext object.
Can someone help me with the XAML and code behind for this scenario?