예제 #1
0
        public void AddElement(T element, T parent, int insertPosition)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element), "element is null");
            }

            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent), "parent is null");
            }

            if (parent.Children == null)
            {
                parent.Children = new List <T>();
            }

            parent.Children.Insert(insertPosition, element);
            element.Parent = parent;

            TreeElementUtility.UpdateDepthValues(parent);
            TreeElementUtility.TreeToList(root, data);

            Changed();
        }
예제 #2
0
        public void AddElements(IList <T> elements, T parent, int insertPosition)
        {
            if (elements == null)
            {
                throw new ArgumentNullException(nameof(elements), "elements is null");
            }

            if (elements.Count == 0)
            {
                throw new ArgumentNullException(nameof(elements), "elements Count is 0: nothing to add");
            }

            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent), "parent is null");
            }

            if (parent.Children == null)
            {
                parent.Children = new List <T>();
            }

            parent.Children.InsertRange(insertPosition, elements.Cast <T>());
            foreach (T element in elements)
            {
                element.Parent    = parent;
                element.TreeDepth = parent.TreeDepth + 1;
                TreeElementUtility.UpdateDepthValues(element);
            }

            TreeElementUtility.TreeToList(root, data);

            Changed();
        }
예제 #3
0
        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();
        }