DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid

后端 未结 8 1564
醉酒成梦
醉酒成梦 2020-12-09 17:03

I\'ve been reading about this at least for 4 hours, and seems to be the list type, but I have a situation:

A ObservableCollection that has a collection property.

8条回答
  •  爱一瞬间的悲伤
    2020-12-09 17:09

    You might try this. 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 may 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 OriginalSource stops that error occurring in its tracks.

提交回复
热议问题