/// <summary> /// Handles child collection changes. /// </summary> /// <param name="parent"> /// The parent. /// </param> /// <param name="e"> /// The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data. /// </param> internal void ChildCollectionChanged(TreeListBoxItem parent, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Move: foreach (var item in e.OldItems) { var container = this.ContainerFromItem(item); if (container != null && container.IsExpanded) { container.IsExpanded = false; } this.Items.Remove(item); } break; case NotifyCollectionChangedAction.Reset: var itemsToRemove = new List<TreeListBoxItem>(); foreach (var item in this.Items) { var container = this.ContainerFromItem(item); if (container == null || !ReferenceEquals(container.ParentItem, parent)) { continue; } itemsToRemove.Add(container); } foreach (var container in itemsToRemove) { if (container.IsExpanded) { container.IsExpanded = false; } this.Items.Remove(container.Content); } parent.IsExpanded = false; parent.HasItems = false; break; case NotifyCollectionChangedAction.Replace: // todo: remove old items and child items throw new NotImplementedException(); } switch (e.Action) { case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Add: if (parent.IsExpanded) { int i; if (e.NewStartingIndex + e.NewItems.Count < parent.Children.Count) { // inserted items var followingChild = parent.Children[e.NewStartingIndex + e.NewItems.Count]; i = this.Items.IndexOf(followingChild); } else { // added items var parentSibling = parent.GetNextSibling(); if (parentSibling == null) { // No sibling found, so add at the end of the list. i = this.Items.Count; } else { // Found the sibling, so add the items before this item. i = this.Items.IndexOf(parentSibling.Content); //// int i0 = this.ItemContainerGenerator.IndexFromContainer(parentSibling); } } if (i < 0) { Debug.WriteLine("TreeListBox bug."); } else { foreach (var item in e.NewItems) { this.parentContainerMap[item] = parent; if (this.Items.Contains(item)) { Debug.WriteLine("TreeListBox bug."); } this.Items.Insert(i++, item); } } } break; case NotifyCollectionChangedAction.Replace: for (int i = 0; i < e.NewItems.Count; i++) { this.Items[e.NewStartingIndex + i] = e.NewItems[i]; } // todo: add new items and child items throw new NotImplementedException(); } parent.HasItems = parent.Children.Cast<object>().Any(); }
/// <summary> /// Inserts the specified items in the tree. /// </summary> /// <param name="parentContainer">The parent container.</param> /// <param name="newItems">The new items.</param> /// <param name="newStartingIndex">New index of the starting.</param> private void InsertItems(TreeListBoxItem parentContainer, IList newItems, int newStartingIndex) { // Find the index where the new items should be added int i; if (newStartingIndex + newItems.Count < parentContainer.Children.Count) { // inserted items var followingChild = parentContainer.Children[newStartingIndex + newItems.Count]; i = this.Items.IndexOf(followingChild); } else { // added items var parentSibling = parentContainer.GetNextSibling(); if (parentSibling == null) { // No sibling found, so add at the end of the list. i = this.Items.Count; } else { // Found the sibling, so add the items before this item. i = this.Items.IndexOf(parentSibling.Content); } } if (i < 0) { #if !DEBUG throw new InvalidOperationException("Could not get parent index in TreeListBox."); #else System.Diagnostics.Trace.WriteLine("Could not get parent index in TreeListBox."); #endif } foreach (var item in newItems) { this.InsertItem(i, item, parentContainer.Content); } }