views:

345

answers:

1

What is the best approach to bind a WPF DataGrid ItemsSource to an ObservableCollection of ObservableCollections ?

e.g.

public ObservableCollection<ObservableCollection<MyDataItem>> DataValues = new ObservableCollection<ObservableCollection<MyDataItem>>();

where MyDataItem may look like this:

public class MyDataItem
{
    public string Caption { get; set; }
    public string DataValue { get; set; }
}

I can assume the collection of collections isn't jagged, and they all contain the same number of "columns"

Is it possible to bind each column dynamically to the 'DataValue' property of the MyDataItem objects or do I need to pack the data into an easier structure to bind to?

A: 

This is possible by using a collection-derived class as the inner object, and implementing ICustomTypeDescriptor on it - See a similar SO question on the topic (tagged with Silverlight, but same idea)

Silverlight DataGrid - Binding to a Collection of Collections of objects.

Aviad P.
Is there a way to do it without modifying the inner object? I get this object hierarchy from a different area of the application and was curious if there is a way to bind directly to it without needing to modify it.
GlitterMonkey
Not that I know of no. You have to expose your inner collection as a single object that has one property for each element of the collection. As far as I can see this can only be achieved by 'wrapping' your inner collection like I suggested. Or if you're inclined to using dynamics (.NET 4.0 feature) - you can implement it in a simpler way (but it'll still be wrapping - i.e defining an extra type instead of the inner collection).
Aviad P.