【发布时间】: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