/// <summary> /// Expands the current branch if possible /// </summary> public void Expand() { if (ChildBranches == null) { FetchChildren(); } if (ChildBranches.Any()) { IsExpanded = true; } }
/// <summary> /// Refreshes cached knowledge in this branch e.g. what children an object has /// </summary> /// <param name="startAtTop">True to also refresh all <see cref="Parent"/> /// branches (starting with the root)</param> public void Refresh(bool startAtTop) { // if we must go up and refresh from the top down if (startAtTop) { Parent?.Refresh(true); } // we don't want to loose the state of our children so lets be selective about how we refresh //if we don't know about any children yet just use the normal method if (ChildBranches == null) { FetchChildren(); } else { // we already knew about some children so preserve the state of the old children // first gather the new Children var newChildren = tree.TreeBuilder?.GetChildren(this.Model) ?? Enumerable.Empty <T> (); // Children who no longer appear need to go foreach (var toRemove in ChildBranches.Keys.Except(newChildren).ToArray()) { ChildBranches.Remove(toRemove); //also if the user has this node selected (its disapearing) so lets change selection to us (the parent object) to be helpful if (Equals(tree.SelectedObject, toRemove)) { tree.SelectedObject = Model; } } // New children need to be added foreach (var newChild in newChildren) { // If we don't know about the child yet we need a new branch if (!ChildBranches.ContainsKey(newChild)) { ChildBranches.Add(newChild, new Branch <T> (tree, this, newChild)); } else { //we already have this object but update the reference anyway incase Equality match but the references are new ChildBranches [newChild].Model = newChild; } } } }
/// <summary> /// Returns true if the current branch can be expanded according to /// the <see cref="TreeBuilder{T}"/> or cached children already fetched /// </summary> /// <returns></returns> public bool CanExpand() { // if we do not know the children yet if (ChildBranches == null) { //if there is a rapid method for determining whether there are children if (tree.TreeBuilder.SupportsCanExpand) { return(tree.TreeBuilder.CanExpand(Model)); } //there is no way of knowing whether we can expand without fetching the children FetchChildren(); } //we fetched or already know the children, so return whether we have any return(ChildBranches.Any()); }