问题
I have a WPF datagrid, and one of the columns is a nested DataGrid:
<DataGridTemplateColumn Header="Description" Width="{Binding DataContext.ColWidths[5].Width, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding oDesc}" AutoGenerateColumns="False"
SelectedItem="{Binding CurrentDesc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewMouseDoubleClick="DataGrid_PreviewMouseDoubleClick" CellStyle="{DynamicResource EmbeddedDataGridCellStyle}">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseDoubleClick" Handler="DataGrid_PreviewMouseDoubleClick"></EventSetter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding RepNumber, Mode=OneWay}" Width="15"/>
<DataGridTextColumn Header=" " Binding="{Binding Description1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*" >
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="SpellCheck.IsEnabled" Value="True"/>
<Setter Property="AcceptsReturn" Value="True"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<i:InvokeCommandAction Command="{Binding DataContext.DescRowEditEnding, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding DataContext.GridDoubleClick, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I get an exception (Sytem.InvalidOperationException: CommitNew is not allowed for this View) from
System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.CommitNew()
which is called from
System.Windows.Controls.DataGrid.CommitRowItem()
None of my code is in the stack (it references back to my app.Run command in the App.g.cs file). The changes are updated to the database, so it happens after a SubmitChanges() call in my ViewModels RowEditEnding handler.
The only reference I find for this error (here) tells me that my CellTemplate override could be messing it up, so I added a copy of the default celltemplate style to my application resources and use it for both data grids to no avail.
I don't know how to go about tracking down what is going on here. Has anyone dealt with this before? I will also add that it seems that this problem is getting worse as the database is growing. It seemed to never happen when I initially deployed this and there were very few items in the database.
Solution: My viewmodel has ObservableCollections that use a CollectionViewSource as well (I call them ViewSourceCollections, which inherits from ObservableCollection and has a ListCollectionView member). This gives me all of the sorting, grouping, and filtering goodness built in that ObservableCollections really don't provide. So the solution presented by @PrashantManjule below can be easily tested in much simpler form:
myViewSourceCollection.View.MoveCurrentToFirst();
in the RowEditEnding. Once I did this for both the main collection as well as the child collection in their respective datagrid RowEditEnding handlers the problem goes away. I still have no idea why this worked at all, and only over time did this start to happen more and more often until finally any edit would cause the exception (and crash).
回答1:
I seen this error in 3 cases
case1 : this error shown if double clicking the datagrid then(custom datagrid which contains processed data like analysis)
Simply, Set in Datagrid IsReadOnly="True"
case2 : this error shown after editing the datagrid, must set during RowEditEnding
(sender as DataGrid).CommitEdit(DataGridEditingUnit.Row);
case3 : this error shown after RowEditEnding event, then must see where the datagrid is reloading the data, it can happen if viewsource or datagrid is already in use and we try to override the data manually
Let me know if any new cases you seen
回答2:
The answer was given by @PrashantManjule but then was edited to be not correct. What I did to solve the issue:
In my viewmodel RowEditEnding handler:
ViewSourceCollection.View.MoveCurrentToFirst();
as the last action of the handler. The database update in the RowEditEnding handler were always working correctly, but this seems to fix the UI exceptions that I was getting.
来源:https://stackoverflow.com/questions/57683346/commitnew-is-not-allowed-for-this-view