NewItemPlaceholder problem

Aug 28, 2009 at 4:52 AM

I have an admin screen application where the user can edit an ObservableCollection<DataItem> in a WPF DataGrid (including add new ones).

And then they can preview the result of their work in a ComboBox.

Unfortunatly, somehow, due to editing the collection in the datagrid, when the list is shown in a combo box, I got a 'NewItemPlaceholder' in the list (in the observable collection that is) which is not a DataItem.

That cause all sorts of exception.. why is that? what can I do to prevent my combobox from crashing the application?

As the datagrid take the initiative on it own (apparently) of silently updating the default collection view of the itemsSource, shouldn't it works with its own ICollectionView instead of using the default one?

Aug 28, 2009 at 8:34 PM

Hi superlloyd,

Ok, so it sounds like you've got your DataGrid and ComboBox bound to the same collection, and since the ComboBox doesn't know what to do with the NewItemPlaceholder, it crashes.  NewItemPlaceholder is something which we add to the DataGrid.Items collection to represent the blank AddNewRow in the DataGrid.  However, NewItemPlaceholder should not be added to the DataGrid.ItemsSource (just the Items collection), so if you bind your ComboBox to DataGrid.ItemsSource, then this should solve the problem. 

If for some reason that doesn't work, a less elegant solution would be to have two separate collections, one for DataGrid (which includes the NewItemPlaceholder) and one for ComboBox (which does not).  Whenever anything is updated or added in the DataGrid's collection, you can manually make those same changes in the ComboBox's collection, which should give the same appearance to the end user of the editing the ComboBox's collection through the DataGrid.


Aug 29, 2009 at 12:46 AM

The ItemsControl.Items is a one to one wrapper on the CollectionViewSource.GetDefaultCollection() or so it seems.

Because while the NewItemPlaceholder was not added to my underlying collection, the combo box which use the default collection by default, as all ItemsControl (including the DataGrid) has it.

The work around I used was to create a 
new ListCollectionView(myCollection)

and use that as the collection for the datagrid.

In fact I had to do that for all collection I was displaying in the datagrid. 

Nov 16, 2010 at 12:08 PM

15 months later I'm having a similar problem, but I'm not willing to create a second collection just to cover-up this bug.  That wouldn't be very MVVM would it?  The best workaround I can come up with is to use a style selector to hide the entry in the ComboBox and hope there's absolutely no way the user can accidently select it.  How can the DataGrid be adding an item to my strongly-typed BindingList which is of the NewItemPlaceholder type?  I guess that it's not, and there's some sort of secret middle tier.  If this is so, why to do controls in the same window share this middle tier rather than having independent bindings to the same object?

Jan 31, 2012 at 11:43 AM

And another 14 months later i'v run into the same problem. In my case i have a wizard style application using pages. On one page i have a DataGrid bound to a DataTable where the user can add/delete/edit items. On the next page i have a ComboBox that is bound to the same DataTable (the user is supposed to select an entry from the page before). If i open the second page directly (with some initial entries to the DataTable) everything is fine. Then i open the page with the DataGrid and don't do anything there. When i go back to the second page the ComboBox now has a new row, the new item placeholder. The user can select it and all sorts of problems arise. And like harmish i'm not willing to have a second DataTable because of this bug.

Jan 31, 2012 at 1:36 PM

Hey Naryoril!


Well, I don't have the code here (new company) but basically I wrote a custom NullableCombox class to use in my datatemplate.

This one was particularly tricky! One problem is the order in which the properties (ItemsSource, SelectedItem) are binded matters!

Apr 5, 2012 at 9:28 AM

I also used a CollectionViewSource (same as superlloyd) for the ComboBox to remove the NewItemPlaceholder items.

Really surprised at this issue - I think it's bad that the DataGrid doesn't deal with the adding of items internally.  It seems a bit flimsy that an extra item is added to the collection before the user adds one.