繁体   English   中英

如何从ObservableCollection中获取IndexOf Removed Item

[英]How do i get IndexOf Removed Item from ObservableCollection

我想用撤销/重做功能覆盖我的ObservableCollection
但是如何获得OldItem的IndexOf,因为它仍然是-1

它看起来像没有CollectionChanging()实现

在删除操作中, NotifyCollectionChangeEventArgsOldStartingIndex属性将告诉您项目在删除之前保留在集合中的索引。

例如

var col = new ObservableCollection<string>();
col.Add("hello");
col.Add("world");
col.CollectionChanged += (sender, e) => Console.WriteLine(e.OldStartingIndex);
col.RemoveAt(1);
col.RemoveAt(0);

这将打印出来

1
0

对我来说,一个解决方案是为您的数据保留一个单独的List,并让ObservableCollection<int>保留序列索引,这将使其轻量化。 您可以将List<ObservableCollection<int>>作为历史记录缓存,并动态更新列表中的当前位置(将其视为导航历史记录)。 请注意,这取决于您的性能要求,如果您的数据非常冗长,这是有问题的,否则这更容易实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM