【问题标题】:CommitNew is not allowed for this View此视图不允许 CommitNew
【发布时间】:2019-08-27 23:35:24
【问题描述】:

我有一个 WPF 数据网格,其中一列是嵌套的 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>

我从

得到一个异常(Sytem.InvalidOperationException: CommitNew is not allowed for this View)
System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.CommitNew()

调用自

System.Windows.Controls.DataGrid.CommitRowItem()

堆栈中没有我的代码(它在 App.g.cs 文件中引用了我的 app.Run 命令)。更改会更新到数据库,因此会在我的 ViewModels RowEditEnding 处理程序中调用 SubmitChanges() 之后发生。

我找到的有关此错误的唯一参考 (here) 告诉我,我的 CellTemplate 覆盖可能会搞砸,因此我将默认 celltemplate 样式的副本添加到我的应用程序资源中,并将其用于两个数据网格没用。

我不知道如何去追踪这里发生的事情。以前有没有人处理过这个问题?我还要补充一点,随着数据库的增长,这个问题似乎越来越严重。当我最初部署它并且数据库中的项目很少时,它似乎从未发生过。

解决方案:我的视图模型也有使用 CollectionViewSource 的 ObservableCollections(我称它们为 ViewSourceCollections,它继承自 ObservableCollection 并具有 ListCollectionView 成员)。这为我提供了 ObservableCollections 真正无法提供的所有排序、分组和过滤功能。所以下面@PrashantManjule 提出的解决方案可以很容易地以更简单的形式进行测试:

myViewSourceCollection.View.MoveCurrentToFirst();

在 RowEditEnding 中。一旦我为各自的数据网格 RowEditEnding 处理程序中的主集合和子集合执行此操作,问题就会消失。我仍然不知道为什么这会起作用,只是随着时间的推移,这种情况开始越来越频繁地发生,直到最后任何编辑都会导致异常(和崩溃)。

【问题讨论】:

    标签: c# wpf linq-to-sql datagrid


    【解决方案1】:

    我在 3 个案例中看到了这个错误

    case1:如果双击数据网格然后显示此错误(自定义数据网格,其中包含已处理的数据,如分析)

    简单地说,在Datagrid中设置IsReadOnly="True"

    case2 : 编辑数据网格后显示此错误,必须在 RowEditEnding 期间设置

      (sender as DataGrid).CommitEdit(DataGridEditingUnit.Row);
    

    case3 : 这个错误在 RowEditEnding 事件之后显示,然后必须查看数据网格在哪里重新加载数据,如果视图源或数据网格已经在使用中并且我们尝试手动覆盖数据,则可能会发生这种情况

    如果您发现任何新病例,请告诉我

    【讨论】:

    • “t_productViewSource”应该是什么?这是 XAML 中数据网格的名称吗?
    • 感谢您的提示。 . .我终于意识到这是什么,只是在正确的位置从我的 ViewModel ViewSourceCollections 中调用它,突然它工作得很好。我希望你能编辑你的答案,为为什么需要这样做添加一些解释,但无论如何我都接受它作为解决方案。
    • 我认为在这个 ViewModel 已经在使用中,尝试再次使用它使其冲突
    • 我想你改变了这个答案。 . .第一个版本更正确:RowEditEnding 处理程序中的 View.MoveCurrentToFirst()。当前答案不正确,因为我要求我的数据网格是可编辑的。
    • 是的,我改变了答案,在双击自定义数据网格时多次出现同样的错误,为了解决它......
    【解决方案2】:

    @PrashantManjule 给出了答案,但随后被编辑为不正确。我做了什么来解决这个问题:

    在我的视图模型 RowEditEnding 处理程序中:

    ViewSourceCollection.View.MoveCurrentToFirst();
    

    作为处理程序的最后一个动作。 RowEditEnding 处理程序中的数据库更新始终正常工作,但这似乎修复了我遇到的 UI 异常。

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 2017-04-10
      • 2013-10-20
      • 1970-01-01
      • 2017-04-26
      • 2011-03-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多