public void MoveElements(T parentElement, int insertionIndex, List <T> elements) { if (insertionIndex < 0) { throw new ArgumentException("Invalid input: insertionIndex is -1, client needs to decide what index elements should be reparented at"); } // Invalid reparenting input if (parentElement == null) { return; } // We are moving items so we adjust the insertion index to accomodate that any items above the insertion index is removed before inserting if (insertionIndex > 0) { insertionIndex -= parentElement.Children.GetRange(0, insertionIndex).Count(elements.Contains); } // Remove draggedItems from their parents foreach (T draggedItem in elements) { draggedItem.Parent.Children.Remove(draggedItem); // remove from old parent draggedItem.Parent = parentElement; // set new parent } if (parentElement.Children == null) { parentElement.Children = new List <T>(); } // Insert dragged items under new parent parentElement.Children.InsertRange(insertionIndex, elements); TreeElementUtility.UpdateDepthValues(Root); TreeElementUtility.TreeToList(root, data); Changed(); }
public void RemoveElements(IList <T> elements) { foreach (T element in elements) { if (element.Equals(root)) { throw new ArgumentException("It is not allowed to remove the root element"); } } IList <T> commonAncestors = TreeElementUtility.FindCommonAncestorsWithinList(elements); foreach (T element in commonAncestors) { element.Parent.Children.Remove(element); element.Parent = default; } TreeElementUtility.TreeToList(root, data); Changed(); }