0
votes

i'm populating a datagrid programmatically but before setting the itemsource, i'm also programmatically adding the datagrid columns.

 DataGridTextColumn col = new DataGridTextColumn();
 col.Header = "MyCol";
 col.Binding = new Binding("PropertyOFObject");
 dataGrid.Columns.Add(col);

it's easy to set the binding to the properties of my object that are concrete however, as a property of this object, i have a list of another object type. now, for each instance of the second object type in that list, i'd like another column to my grid, populated with a specific property of that instance of the second object type.

how would i go about doing that in this same fashion of programmatically adding the columns and setting the bindings?

1
I have a grid. I have an object "A". A has properties, one of which is a "List<T>" property. This list property is a list of instances of object "B". For each property, I'm programmatically adding and binding a column in my grid. I would like to do this same thing for each instance of object "B" in the list property of object "A". These columns would be added to the same grid.ghost_mv

1 Answers

1
votes

If you want to bind the items of the child property to columns you can create a foreach loop which creates dynamic bindings, in one WPF question i gave an example for arrays this should be rather similar.

The key is to use a for-loop over the length of the list and creating property-paths with injected indexer:

new Binding("Property[" + i + "]")