ObservableList

ObservableList<T> erweitert ObservableCollection<T> mit nützlichen Methoden, um einfach mehrere Elemente in die Collection hinzufügen oder entfernen.

Anwendungs-Beispiele:

var items1 = new string[] { "a", "b", "c" };
var items2 = new string[] { "x", "y", "z" };
var items3 = new string[] { "1", "2", "3" };

// Adds a, b, c to the end of collection.
Items.InsertItemsAtEnd(items1);

// Adds x, y, z to start of collection.
Items.InsertItemsAtStart(items2);

// Adds 1, 2, 3 to indices 3, 4 and 5.
Items.InsertItemsAt(3, items3);

// Removes first 4 elements from collection.
Items.RemoveItemsFromEnd(4);

// Removes last 4 elements from collection.
Items.RemoveItemsFromStart(4);

// Adds Hello and World to the end of collection.
Items.AddRange(new string[] { "Hello", "World!"});

Implementierung von ObservableList<T>:

public class ObservableList<T> : ObservableCollection<T>
{
    public virtual void RemoveItems(IEnumerable<int> pIndices)
    {
        foreach (var i in pIndices)
            RemoveItem(i);
    }

    public virtual void RemoveItemsFromStart(int pCount)
    {
        while (Count > 0 && pCount > 0)
        {
            RemoveAt(0);
            pCount--;
        }
    }

    public virtual void RemoveItemsFromEnd(int pCount)
    {
        while (Count > 0 && pCount > 0)
        {
            RemoveAt(Count - 1);
            pCount--;
        }
    }

    public virtual void InsertItemsAt(int pIndex, IEnumerable<T> pItems)
    {
        foreach(var item in pItems)
            InsertItem(pIndex++, item);
    }

    public virtual void InsertItems(IEnumerable<T> pItems)
        => InsertItemsAt(Count, pItems);

    public virtual void InsertItemsAtStart(IEnumerable<T> pItems)
        => InsertItemsAt(0, pItems);

    public virtual void InsertItemsAtEnd(IEnumerable<T> pItems)
        => InsertItems(pItems);

    public virtual void AddRange(IEnumerable<T> pItems)
        => InsertItems(pItems);
}