views:

105

answers:

1

I'm using DataGrid from CodePlex.

[Summary: How to use bind dynamic data (a property of an object specified by dynamic name) when using template selectors? Alternative solutions would be accepted as well. I've been thinking about making my own based on BoundColumn, but someone was having problems with that approach.]

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

Found a post by Wang, Jie which helped to solve my problem.

Adding a name property to DataGridTemplateColumn and overriding GenerateElement and GenerateEditingElement and changing the default binding to the name property (within the generation) does the trick.

Now I can set the propertyname in the columns and the datatemplates (and selectors) work as expected.

See Social.MSDN for details if needed.

TuomoT