Removing items from DataGrid ItemsSource hides NewItemPlaceholder

Nov 12, 2008 at 8:05 PM
Edited Nov 12, 2008 at 9:28 PM

I am having an issue where if I programmatically remove an item from my DataGrid's ItemsSource, the NewItemPlaceholder disappears. Specifically, the scenario is that when a user double-clicks the placeholder to add an item to the DataGrid, the control creates a new item in the ItemsSource collection. If the user then hits Escape to cancel the edit, the new item should be deleted from the ItemsSource collection, but it is not. When I attempt to clean up manually by adding a handler on the CellEditEnding event which removes the item from the ItemsSource, the NewItemPlaceholder disappears as soon as I call the Remove method of the ItemsSource. Do you have any ideas on how to fix this? My objects need access to some state information, so I have an InitializingNewItem handler to set this for each new item. Could this be what is preventing the DataGrid control from removing the new item from the ItemsCollection when the user cancels a "new item" edit?


--Brandon Siegel

UPDATE: I tried converting my ItemsSource from an ObservableCollection to a BindingList (which implements the ICancelAddNew interface), but the same issue presents itself. Cancelling the edit by hitting Escape does not automatically call the CancelNew method of my BindingList, and manually calling it from the CellEditEnding handler causes the NewItemPlaceholder to disappear. I'm quickly running out of ideas.
Nov 13, 2008 at 1:47 PM
When you press Escape twice it will remove the row.  You do not want that behavior?
Nov 13, 2008 at 3:04 PM
(I was bugmenot2 - finally got my activation e-mail)

First off, I am pleased to report that vinsibal's hint works. If I hit Escape to cancel creating a new entry in the DataGrid, and then press Escape again, the second Escape deletes the new item from the ItemsSource. However, this seems extremely unintuitive. If the user types in some data and then presses escape, the data remains visible in the DataGrid. It is only when the user presses Escape a second time does the exteraneous DataGrid line disappear. Is there a way to have this behavior occur with the first press of the Escape key?


--Brandon Siegel
Nov 13, 2008 at 3:42 PM
The reason it involves two escapes instead of one is for cancel cell edits and cancelling row edits.  For example,

1. edit cell 0
2. tab to the next cell and edit cell 1.
3. press Esc => cell 1 reverts back, cell 0 remains dirty.
4a. press Esc => cell 0 reverts back.
4b. press Enter => cell 0 commits.

In the sample on this post,, it highlights the borders of the row and cell when editing.  This should give you a better visualization of the rows and cells when in edit mode.

Anyway, if you really want to cancel the entire row on the first escape you can listen for the "Esc" key input and call DataGrid.CancelEdit(DataGridEditingUnit.Row). 
Nov 13, 2008 at 4:21 PM
Thanks, Vincent! That worked perfectly. My DataGrid has only one column, so that is why I didn't see what was really going on.

--Brandon Siegel