我有一个DataGrid
绑定到ObservableCollection
的DataRowView
。
当我执行任何操作(如插入/删除/复制/粘贴/撤消/重做)时,我将操纵底层的数据源。
例如:当我必须将数据粘贴到一组单元格中时,我将获得它们的行和列索引(上下文菜单所指向的单元格)并编辑数据源。我存储它们的先前值和索引以在执行“撤消”操作时还原。
当我过滤网格时,视图将更改为与数据源相同的位置。因此,当我执行粘贴操作时,得到的索引与数据源中的索引不匹配。我最终粘贴了错误的单元格。
如何在应用过滤器后操纵数据源,即获取实际的索引/数据来执行所有操作?
根据我对您提出的问题的理解。出现在Item1之后的新记录也应该出现在源集合的Item1之后。由于索引不同(当然会有所不同),因此目前还没有发生这种情况。由于您尚未发布代码,因此我在下面发布了示例代码:
// new employee to add
Employee empNew = new Employee() { Name = "New1", Address = "NewAdd1" };
// get corresponding item in filtered view after which you want to insert
Employee emp = (Employee)DgrdEmployees.SelectedItem;
// get true collection which is datasource
ObservableCollection<Employee> sourceCol = (ObservableCollection<Employee>)(CollectionViewSource.GetDefaultView(DgrdEmployees.ItemsSource).SourceCollection);
// find index of selected item after which we are inserting/pasting
int trueIndex = sourceCol.IndexOf(emp);
// insert it at exact location in true unfiltered source collection
sourceCol.Insert(trueIndex + 1, empNew);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句