How to specify default type for new DataGridRow's Item?

Apr 1, 2009 at 9:26 PM

I am facing a problem with adding new items with the WPF DataGrid.  I am currently binding to an ObservableCollection<Object>.  When I attempt to create/add a new item, an object of type Object is added to my collection while setting the current DataGridRow's Item property to some default Object.  Is there any way to specify a default type for new rows, other than specifying the type through the ObservableCollection's type parameter?

* For anyone wondering why I would use an ObservableCollection<Object>:  Everything was working great when I was using an ObservableCollection<Product> (where Product is one of my Linq-toSQL types), but I wanted to switch to ObservableCollection<Object> so that I could create a generic ObjectDataProvider that would be used like the following:

        <ObjectDataProvider x:Key="ProductProvider" ObjectType="{x:Type local:GenericEntityProvider}" >
                <x:Type Type="data:Product" />
        <ObjectDataProvider x:Key="Products" ObjectInstance="{StaticResource ProductProvider}" MethodName="GetEntities"/>

By specifying the type in the ConstructorParameters, I can get whatever type of entities I need, whithout having to write another class or method for each type of entity I'll want to edit.
Apr 3, 2009 at 9:28 PM
I dont think that is possible today as such. But I think you can achieve this by handling RowEditEnding event and cancel the edit when it is for add and add your custom object instead.

In RowEditEnding handler, the operation is add if

IEditableCollectionView ecv = dataGrid.Items as IEditableCollectionView;
if(ecv != null & ecv.AddingNew)
    // the operation is add