How can I bind dynamic data to DataGridTemplateColumn in code?

Posted by TuomoT on Stack Overflow See other posts from Stack Overflow or by TuomoT
Published on 2010-06-07T08:43:16Z Indexed on 2010/06/07 9:42 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

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);
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf