DataGrid binding to collection

Nov 11, 2008 at 6:49 PM
I'm trying to use the new DataGrid to bind to an ObservableCollection<T>...

I'm setting ItemsSource to the collection, and if my class T has a public constructor, I can add new rows at the bottom of the grid. However, I wish to use a class for T that doesn't have a public default constructor, but does have a factory that can create new instances of it. Consequently, I can't add new rows to the grid.

Anyone know if there is there a way to tell the grid to use a provided class factory, rather than requiring a default constructor?
Nov 13, 2008 at 3:22 PM
Currently there isn't a way to do this with the current IEditableCollectionView.  There have been some talks to enable this scenario and if it were to go through you won't see it until vNext or vNextNext.  From the DataGrid, one thing you can do is listen the OnInitializingNewItem event or simply create the item yourself and add it to the collection.
Nov 13, 2008 at 3:52 PM
I don't get the event, as the grid has already decided it doesn't allow adding new items?
Nov 13, 2008 at 5:24 PM
So you mean IEditableCollectionView.CanAddNew returns false?  If that's the case, then you can't add a new item regardless.
Nov 14, 2008 at 2:18 PM
Yes, IEditableCollectionView.CanAddNew is returning false. This is because the binding to my ObservableCollection is assuming it can't create items of T because there is no default constructor. Work is probably needed in the implementation of IEditableCollectionView to support other T creation mechanisms.

As a temporary workaround, I've added a button to my form to add rows to the collection explicitly...
Oct 7, 2009 at 9:28 AM

Any updates to whether this scenario is supported?