0

我的视图模型中有一个ICollectionView绑定到DataGrid视图中。当我执行添加/删除操作时。我得到了这个例外。

我无法更改CollectionView. 谁能告诉我这个例外的原因,这样我就可以找到解决方案。

例外:

“添加的项目未出现在给定索引处”

堆栈跟踪:

在 System.Windows.Data.ListCollectionView.AdjustBefore(NotifyCollectionChangedAction 操作,对象项,Int32 索引) 在 System.Windows.Data.ListCollectionView.CommitNew() 在 System.Windows.Controls.DataGrid.CommitRowItem() 在 System.Windows.Controls .DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding) 在 System.Windows。 Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute) at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command,Boolean execute) at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated) at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand命令,DataGridCell cellContainer,DataGridEditingUnit editingUnit,布尔 exitEditMode) 在 System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)System.Windows.EventRoute.InvokeHandlersImpl 的 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)的 OnExecuted(对象发送者,ExecutedRoutedEventArgs e)(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl( DependencyObject sender, RoutedEventArgs args) at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated) at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode) at System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)System.Windows.EventRoute.InvokeHandlersImpl 的 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)的 OnExecuted(对象发送者,ExecutedRoutedEventArgs e)(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl( DependencyObject sender, RoutedEventArgs args) at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated) at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode) at System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)对象目标)在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数)在 System.Windows.Input.RoutedCommand.ExecuteImpl(对象参数, System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)处 System.Windows.Controls.DataGrid.EndEdit(RoutedCommand 命令,DataGridCell cellContainer,DataGridEditingUnit 编辑单元,布尔 exitEditMode)处的 IInputElement 目标,布尔 userInitiated)对象目标)在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数)在 System.Windows.Input.RoutedCommand.ExecuteImpl(对象参数, System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)处 System.Windows.Controls.DataGrid.EndEdit(RoutedCommand 命令,DataGridCell cellContainer,DataGridEditingUnit 编辑单元,布尔 exitEditMode)处的 IInputElement 目标,布尔 userInitiated)System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode) 在 System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e )System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode) 在 System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e )

谢谢

4

1 回答 1

1

虽然我有同样的问题“添加的项目没有出现在给定的索引 '0' 我正在搜索它并找到了这个未解决的帖子。

在我的个人文件中,我向collescion asynchon 提供数据,所以我要做的就是在其中添加一个SynchronizationContext。

我们就是这样,在我用结果填充 observablecollection 之前,我只需要:

 _synchronizationContext.Acquire();
 (ObservableCollectionName).Add(YourItem);
 _synchronizationContext.Release();

即使这是一篇旧帖子,我希望它至少可以帮助其他人:=)

于 2017-01-25T12:47:40.587 回答