Observable Collection replace item

Viewed 30290

I have a ObservableCollection, I can add and remove item from the collection. But I can't replace an existing item in the collection. There is a way to replace an item and reflect that on my bound components.

System.Collections.Specialized.NotifyCollectionChangedAction.Replace

Can anyone please show me how to accomplish this?

3 Answers

Simple extension method for replace item in observable collection:

public static void ReplaceItem<T>(this ObservableCollection<T> items, Func<T, bool> predicate, T newItem)
{
    for (int i = 0; i < items.Count; i++)
    {
        if (predicate(items[i]))
        {
            items[i] = newItem;
            break;
        }
    }
}
Related