【问题标题】:Reason for exception - added item does not appear at given index异常原因 - 添加的项目未出现在给定索引处
【发布时间】:2013-09-09 08:57:07
【问题描述】:

我的视图模型中有一个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(对象发送者,ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandManager.ExecuteCommandBinding(对象发送者,ExecutedRoutedEventArgs e,CommandBinding commandBinding) 在 System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings,对象发送者,RoutedEventArgs e,ICommand 命令,布尔执行) 在 System.Windows.Input.CommandManager.FindCommandBinding(对象发送者,RoutedEventArgs e,ICommand 命令,布尔执行) 在 System.Windows.Input.CommandManager.OnExecuted(对象发送者,ExecutedRoutedEventArgs e) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs 参数,布尔 reRaised) 在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数) 在 System.Windows.Input.RoutedCommand.ExecuteImpl(对象参数,IInputElement 目标,布尔用户启动) 在 System.Windows.Controls.DataGrid.EndEdit(RoutedCommand 命令,DataGridCell cellContainer,DataGridEditingUnit 编辑单元,布尔 exitEditMode) 在 System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

谢谢

【问题讨论】:

  • 你得到什么异常?好像你在途中丢失了一些东西。
  • “添加的项目没有出现在给定的索引处”是我得到的例外。

标签: c# wpf exception


【解决方案1】:

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

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

所以在我用结果填充 observablecollection 之前,我只需要:

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

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

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多