I am Binding a Datagrid to dynamic data via IDictionary: http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-8681
But I do not want to define any columns in the xaml (below is how it is done in Colin Eberhardt's post
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Forename" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=Forename}"/>
So I have written the following code to try and do the same thing in the code behind, but the code does not call the RowIndexConverter... something must be missing.
Code in question
// add columns
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "Forename";
Binding bind = new Binding("Forename");
bind.Converter = new RowIndexConverter() ;
bind.ConverterParameter = "Forename";
textColumn.Binding = bind;
_dataGrid.Columns.Add(textColumn);
...
Rest of Code (Here for context)
// generate some dummy data
Random rand = new Random();
for (int i = 0; i < 200; i++)
{
Row row = new Row();
row["Forename"] = s_names[rand.Next(s_names.Length)];
row["Surname"] = s_surnames[rand.Next(s_surnames.Length)];
row["Age"] = rand.Next(40) + 10;
row["Shoesize"] = rand.Next(10) + 5;
_rows.Add(row);
}
// bind to our DataGrid
_dataGrid.ItemsSource = _rows;
public class Row
{
private Dictionary<string, object> _data = new Dictionary<string, object>();
public object this[string index]
{
get
{
return _data[index];
}
set
{
_data[index] = value;
}
}
}