public void RebuildList(bool overrideIncludeRoot = false) { this.RemoveAllHandlers(); // Build the list from scratch - add the rootItem and its handlers, and then all the children if any. this.excludedRootItem = null; this.itemList.Clear(); ObservableTreeNode rootItem = _rootNode; if (rootItem != null) { this.AddHandlers(rootItem); if (this.includeRoot && !overrideIncludeRoot) { this.itemList.Add(rootItem); } else { this.excludedRootItem = rootItem; } // if not including the root, always expand if (rootItem.IsExpanded || !this.includeRoot) { // if we're including the root in the list, we want to start // inserting children after it (so index 1, root is in 0); // if no root, start adding children at the beginning of the list int index = this.includeRoot ? 1 : 0; this.InsertChildrenInList(rootItem, ref index); } } }
private void SetRootNode(VariableViewModel evaluation) { _rootNode = new ObservableTreeNode( new VariableNode(_settings, evaluation), Comparer <ITreeNode> .Create(Comparison)); RootTreeGrid.ItemsSource = new TreeNodeCollection(_rootNode).ItemList; }
public TreeNodeCollection( ObservableTreeNode rootNode, bool includeRoot = false) { _rootNode = rootNode; this.itemList = new ObservableCollection <ObservableTreeNode>(); this.readonlyList = new ReadOnlyObservableCollection <ObservableTreeNode>(this.itemList); this.includeRoot = includeRoot; this.RebuildList(); // Expand root node always _rootNode.IsExpanded = true; }
private void RemoveChildrenFromList(ObservableTreeNode parentItem) { int startIndex = this.itemList.IndexOf(parentItem) + 1; int endIndex = startIndex; while (endIndex < this.itemList.Count && this.itemList[endIndex].Depth > parentItem.Depth) { ++endIndex; } for (int i = endIndex - 1; i >= startIndex; --i) { this.RemoveHandlers(this.itemList[i]); this.itemList.RemoveAt(i); } }
private void InsertChildrenInList(ObservableTreeNode parentItem, ref int index) { foreach (var childItem in parentItem.Children) { if (childItem.IsVisible) { this.AddHandlers(childItem); this.itemList.Insert(index, childItem); index++; if (childItem.IsExpanded) { this.InsertChildrenInList(childItem, ref index); } } } }
/// <summary> /// add a direct child node /// </summary> /// <param name="newItem">a node to be added</param> public void AddChild(ObservableTreeNode newItem) { if (newItem == null || newItem.Parent != null) { throw new ArgumentException("Null tree node or a tree with non-null Parent can't be added as a child"); } newItem.Parent = this; int insertionIndex = this.Children.BinarySearch(newItem, _comparer); if (insertionIndex < 0) { // BinarySearch returns bitwise complement if not found insertionIndex = ~insertionIndex; } Children.Insert(insertionIndex, newItem); }
/// <remarks> /// Assumes new data (update) and current Children are sorted in same order /// </remarks> private void UpdateChildren(IReadOnlyList <ITreeNode> update) { // special case of no update if (update == null || update.Count == 0) { RemoveChildren(); return; } var sortedUpdate = update.ToList(); sortedUpdate.Sort(_modelComparer); int srcIndex = 0; int updateIndex = 0; while (srcIndex < Children.Count) { int sameUpdateIndex = -1; for (int u = updateIndex; u < sortedUpdate.Count; u++) { if (Children[srcIndex].Model.CanUpdateTo(sortedUpdate[u])) { sameUpdateIndex = u; break; } } if (sameUpdateIndex != -1) { int insertIndex = srcIndex; for (int i = updateIndex; i < sameUpdateIndex; i++) { var newItem = new ObservableTreeNode(sortedUpdate[i], _modelComparer); newItem.Parent = this; Children.Insert(insertIndex++, newItem); srcIndex++; } Children[srcIndex].Model = sortedUpdate[sameUpdateIndex]; srcIndex++; updateIndex = sameUpdateIndex + 1; } else { RemoveChild(srcIndex); } } if (updateIndex < sortedUpdate.Count) { Debug.Assert(srcIndex == Children.Count); int insertIndex = srcIndex; for (int i = updateIndex; i < sortedUpdate.Count; i++) { var newItem = new ObservableTreeNode(sortedUpdate[i], _modelComparer); newItem.Parent = this; Children.Insert(insertIndex++, newItem); } } }
/// <summary> /// remove a direct child node, and all children, of course /// </summary> /// <param name="index">direct child index</param> public void RemoveChild(ObservableTreeNode child) { child.RemoveChildren(); Children.Remove(child); child.Parent = null; }
private void RemoveHandlers(ObservableTreeNode treeItem) { treeItem.PropertyChanged -= VirtualizingTreeItem_PropertyChanged; treeItem.Children.CollectionChanged -= VirtualizingTreeItem_ChildrenCollectionChanged; }