ArgumentOutOfRangeException при замене элементов в ObservableCollection <T> - PullRequest
3 голосов
/ 02 ноября 2009

Я работаю над методом расширения Refresh () для ObservableCollection, который добавляет, удаляет или заменяет элементы на основе соответствующего ключа (это означает, что при привязке к 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);
    }
}

1 Ответ

2 голосов
/ 02 ноября 2009

У вас есть Старое и Новое неправильно. Это:

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 ...

...