I don't know if this will help, but here's a really simple implementation of an "observable set" that I made for a personal project. It essentially guards against inserting (or overwriting with) an item that is already in the collection.
If you wanted to you could simply return out of the methods rather than throwing an exception.
public class SetCollection<T> : ObservableCollection<T>
{
protected override void InsertItem(int index, T item)
{
if (Contains(item)) throw new ItemExistsException(item);
base.InsertItem(index, item);
}
protected override void SetItem(int index, T item)
{
int i = IndexOf(item);
if (i >= 0 && i != index) throw new ItemExistsException(item);
base.SetItem(index, item);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…