// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System.Collections.Generic; using System.Collections.ObjectModel; namespace System.Windows.Controls.DataVisualization { /// /// An observable collection that does not allow duplicates. /// /// The type of items in the collection. internal class UniqueObservableCollection : ObservableCollection { /// /// Inserts an item at an index. Throws if the item already exists in the collection. /// /// The index at which to insert the item. /// The item to insert. protected override void InsertItem(int index, T item) { if (!this.Contains(item)) { base.InsertItem(index, item); } else { throw new InvalidOperationException(Properties.Resources.UniqueObservableCollection_InvalidAttemptToInsertADuplicateItem); } } /// /// Sets an item at a given index. Throws if the item already exists at another index. /// /// The index at which to insert the item. /// The item to be inserted. protected override void SetItem(int index, T item) { int newItemIndex = this.IndexOf(item); if (newItemIndex != -1 && newItemIndex != index) { throw new InvalidOperationException(Properties.Resources.UniqueObservableCollection_InvalidAttemptToInsertADuplicateItem); } else { base.SetItem(index, item); } } /// /// Clears all items in the collection by removing them individually. /// protected override void ClearItems() { IList items = new List(this); foreach (T item in items) { Remove(item); } } } }