I am trying to do data binding in WPF on data grid using a cutom list. My custom list class contains a private data list of type List. I can not expose this list however the
Just to add my own observation. I had a datagrid with specifically defined columns in Xaml and its ItemsSource set to a simple dictionary. When I tried to edit the second column, I got this exception referring to the dictionary. I then set the data grid ItemsSource to a list of the Keys (dataGrid.Keys.ToList()). I could then edit the second column. It seems a list view allows an 'EditItem'.
edit: Did a little bit more digging into this. I set up a BeginningEdit handler and started poking around. One thing I noticed was that every single time I got this error, EditingEventArgs.Source was a Border. If I can find the time, I may look into this one down a bit further. Also, on one instance, my converting the dictionary keys to a List did not work. I had to convert it to an Observable collection, despite the fact that a List was suitable in all other places in my code where I was doing essentially an identical type of assignment.
edit again: Ok, I have another fix for those for which using an IList type doesn't work. Attach a BeginningEdit handler to your DataGrid and point to this code:
private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
//// Have to do this in the unusual case where the border of the cell gets selected
//// and causes a crash 'EditItem is not allowed'
e.Cancel = true;
}
This will only hit if you somehow manage to physically tap down on the border of the cell. The event's OriginalSource is a Border, and I think what my happen here is instead of a TextBox, or other editable element being the source as expected, this un-editable Border comes through for editing, which causes an exception that is buried in the 'EditItem is not allowed' exception. Canceling this RoutedEvent before it can bubble on through with its invalid Original Source stops that error occurring in its tracks.
Glad to have found this as there was, in my case, a DataGrid on which I couldn't use an IList type.