/// <summary> /// Adds an item to the collection. /// </summary> /// <param name="item">The item to add.</param> public override void Add(ObservableRecursiveCollection <T> item) { base.Add(item); OnPropertyChanged(nameof(Count)); OnPropertyChanged(Binding.IndexerName); //Note: We must cast the item to type 'object' so that the correct overload is called, since our list items are also collections. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (object)item)); item.Parent = this; }
/// <summary> /// Inserts an item into the collection at the specified index. /// </summary> /// <param name="index">The index at which to insert the item.</param> /// <param name="item">The item to insert into the collection.</param> public override void Insert(int index, ObservableRecursiveCollection <T> item) { //Note: No error handling necessary. // If the index is out of bounds, then let the list's insert method throw an exception. base.Insert(index, item); OnPropertyChanged(nameof(Count)); OnPropertyChanged(Binding.IndexerName); //Note: We must cast the item to type 'object' so that the correct overload is called, since our list items are also collections. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (object)item, index)); item.Parent = this; }
/// <summary> /// Removes the first occurrence of a specific item from the collection. /// </summary> /// <param name="item">The item to remove from the collection.</param> /// <returns> /// Returns true if the item was successfully removed from the collection, otherwise false. /// This method also returns false if item was not found in the collection. /// </returns> public override bool Remove(ObservableRecursiveCollection <T> item) { bool removed = base.Remove(item); if (removed) { OnPropertyChanged(nameof(Count)); OnPropertyChanged(Binding.IndexerName); //Note: We must cast the item to type 'object' so that the correct overload is called, since our list items are also collections. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (object)item)); if (item.Parent == this && !Contains(item)) { item.Parent = null; } } return(removed); }
/// <summary> /// Removes all items from the collection. /// </summary> public override void Clear() { var oldItems = new ObservableRecursiveCollection <T> [Count]; ListItems.CopyTo(oldItems, 0); base.Clear(); OnPropertyChanged(nameof(Count)); OnPropertyChanged(Binding.IndexerName); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); foreach (var item in oldItems) { if (item.Parent == this) { item.Parent = null; } } }
/// <summary> /// Creates a new instance of <see cref="ObservableRecursiveCollection{T}"/>. /// </summary> /// <param name="value"> /// The value at this node of the <see cref="ObservableRecursiveCollection{T}"/>. /// </param> /// <param name="parent"> /// The collection of items that is the hierarchical parent of this collection. /// </param> /// <param name="childItems"> /// A collection of value to initialize this collection with, as /// the hierarchical children of this node. /// </param> public ObservableRecursiveCollection(T value, ObservableRecursiveCollection <T> parent, params T[] childItems) { Value = value; Parent = parent; Children = new List <ObservableRecursiveCollection <T> >(childItems.Select(childItem => new ObservableRecursiveCollection <T>(childItem, this))); }
/// <summary> /// Creates a new instance of <see cref="ObservableRecursiveCollection{T}"/>. /// </summary> /// <param name="value"> /// The value at this node of the <see cref="ObservableRecursiveCollection{T}"/>. /// </param> /// <param name="parent"> /// The collection of items that is the hierarchical parent of this collection. /// </param> public ObservableRecursiveCollection(T value, ObservableRecursiveCollection <T> parent) { Value = value; Parent = parent; Children = new List <ObservableRecursiveCollection <T> >(); }