替换 ObservableCollection中的项目时出现 ArgumentOutOfRangeException

发布于 2024-08-09 20:35:32 字数 1453 浏览 6 评论 0原文

我正在为 ObservableCollection 开发 Refresh() 扩展方法,该方法根据匹配键添加、删除或替换项目(这意味着当绑定到 DataGrid 时,网格不会重新滚动,并且项目不会更改其位置,除非它们已被删除)。

问题是当我替换 ObservableCollection 中的项目时,最后一个项目抛出 ArgumentOutOfRangeException,我在这里缺少什么?

public static void Refresh<TItem, TKey>(this ObservableCollection<TItem> target, IEnumerable<TItem> source, Func<TItem, TKey> keySelector)
{
    var sourceDictionary = source.ToDictionary(keySelector);
    var targetDictionary = target.ToDictionary(keySelector);

    var newItems = sourceDictionary.Keys.Except(targetDictionary.Keys).Select(k => sourceDictionary[k]).ToList();
    var removedItems = targetDictionary.Keys.Except(sourceDictionary.Keys).Select(k => targetDictionary[k]).ToList();
    var updatedItems = (from eachKey in targetDictionary.Keys.Intersect(sourceDictionary.Keys)
                        select new
                        {
                            Old = targetDictionary[eachKey],
                            New = sourceDictionary[eachKey]
                        }).ToList();

    foreach (var updatedItem in updatedItems)
    {
        int index = target.IndexOf(updatedItem.Old);
        target[index] = updatedItem.New; // ArgumentOutOfRangeException is thrown here
    }

    foreach (var removedItem in removedItems)
    {
        target.Remove(removedItem);
    }

    foreach (var newItem in newItems)
    {
        target.Add(newItem);
    }
}

I'm working on a Refresh() extension method for ObservableCollection which adds, removes or replaces items based on a matching key (this means when bound to a DataGrid the grid doesn't re-scroll and items don't change their position unless they were removed).

Problem is when I replace items in the ObservableCollection the last item throws an ArgumentOutOfRangeException, what am I missing here?

public static void Refresh<TItem, TKey>(this ObservableCollection<TItem> target, IEnumerable<TItem> source, Func<TItem, TKey> keySelector)
{
    var sourceDictionary = source.ToDictionary(keySelector);
    var targetDictionary = target.ToDictionary(keySelector);

    var newItems = sourceDictionary.Keys.Except(targetDictionary.Keys).Select(k => sourceDictionary[k]).ToList();
    var removedItems = targetDictionary.Keys.Except(sourceDictionary.Keys).Select(k => targetDictionary[k]).ToList();
    var updatedItems = (from eachKey in targetDictionary.Keys.Intersect(sourceDictionary.Keys)
                        select new
                        {
                            Old = targetDictionary[eachKey],
                            New = sourceDictionary[eachKey]
                        }).ToList();

    foreach (var updatedItem in updatedItems)
    {
        int index = target.IndexOf(updatedItem.Old);
        target[index] = updatedItem.New; // ArgumentOutOfRangeException is thrown here
    }

    foreach (var removedItem in removedItems)
    {
        target.Remove(removedItem);
    }

    foreach (var newItem in newItems)
    {
        target.Add(newItem);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-08-16 20:35:32

你把旧的和新的搞错了。这:

var updatedItems = (from eachKey in targetDictionary.Keys
                                              .Intersect(sourceDictionary.Keys)
                    select new
                    {
                        Old = targetDictionary[eachKey],
                        New = sourceDictionary[eachKey]
                    }).ToList();

应该是这样:

var updatedItems = (from eachKey in targetDictionary.Keys
                                              .Intersect(sourceDictionary.Keys)
                    select new
                    {
                        New = targetDictionary[eachKey],
                        Old = sourceDictionary[eachKey]
                    }).ToList();

当前您正在寻找值的索引,该索引将为-1...

You've got Old and New the wrong way round. This:

var updatedItems = (from eachKey in targetDictionary.Keys
                                              .Intersect(sourceDictionary.Keys)
                    select new
                    {
                        Old = targetDictionary[eachKey],
                        New = sourceDictionary[eachKey]
                    }).ToList();

should be this:

var updatedItems = (from eachKey in targetDictionary.Keys
                                              .Intersect(sourceDictionary.Keys)
                    select new
                    {
                        New = targetDictionary[eachKey],
                        Old = sourceDictionary[eachKey]
                    }).ToList();

Currently you're looking for the index of the new value, which will be -1...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文