How can I bind dynamic data to DataGridTemplateColun in code?
- by TuomoT
I'm using DataGrid from CodePlex.
I have objects (ICustomTypeDescriptor) that have properties 'name' 'description' 'c' 'd' and so on (the names are dynamic, and so is the value) and they have information behind them.
I have templates for different types. And if I have to list the properties of a single element I can just bind to the values in xaml and use datatemplateselector (in datagridtemplatecolumn) which will pick the correct template.
However, when I'm listing several of the elements, how can I use the selectors and the templates? How do I get the correct property bound to the template (two-way)?
When ignoring the need for different templates, using DataGridTextColumn with bindings is very simple (and works, see example below), is there any way to use similar approach with the DataGridTemplateColumn?
foreach (String propertyName in listOfPropertyNames)
{
DataGridTextColumn textColumn = new DataGridTextColumn()
{
Header = propertyName,
Binding = new Binding()
{
Path = new PropertyPath(propertyName)
}
};
DataGrid.Columns.Add(textColumn);
}