public void AddSibling() { // Instanciate a new object ControlObjectModel controllerItem = new ControlObjectModel { ID = Guid.NewGuid(), Project_ID = Globals.Project_ID, ObjectName = "New Object", Description = "New Object Description", IsChanged = false, IsNew = true, ControlObjectType_ID = TypeViewModelLocator.GetTypeVM().GetTypeGroupID("Object"), ChildControlObjects = new TD.ObservableItemCollection <ControlObjectModel>() }; // If no item has been selected, put the object in the root of the tree if (SelectedItem == null) { controllerItem.Parent_ID = null; ControlObjects.Add(controllerItem); } // If the selected item is in the root, put the new object in the root also else if (SelectedItem.Parent_ID == null) { controllerItem.Parent_ID = null; controllerItem.ControlObjectType_ID = SelectedItem.ControlObjectType_ID; ControlObjects.Insert(ControlObjects.IndexOf(SelectedItem) + 1, controllerItem); } // Otherwise get the parent object and add the new object as a child else { ControlObjectModel parentItem = GetController(SelectedItem.Parent_ID); controllerItem.Parent_ID = SelectedItem.Parent_ID; controllerItem.ControlObjectType_ID = SelectedItem.ControlObjectType_ID; parentItem.ChildControlObjects.Insert(parentItem.ChildControlObjects.IndexOf(SelectedItem) + 1, controllerItem); } IsChanged = true; OnFocusRequested("ObjectName"); }
/// <summary> /// Method triggered by the TreeListViewDragDropBehavior Class. Takes care of moving on item in the tree, which can be from /// any level to any level /// </summary> /// <param name="destination"></param> public void MoveSelection(TreeListViewRow destination) // Collection<ControlObjectModel> selectedItems, ControlObjectModel destination ) { if (destination != null) { ControlObjectModel destinationItem = (destination.DataContext) as ControlObjectModel; try { // Setup a private collection with the selected items only. This is because the SelectedItems that are part of the view model collection // will change as soon as we start removing and adding ControlObject TD.ObservableItemCollection <ControlObjectModel> selectedItems = new TD.ObservableItemCollection <ControlObjectModel>(); foreach (ControlObjectModel item in SelectedItems) { selectedItems.Add(item); } foreach (ControlObjectModel item in selectedItems) { // find the original parent of the object that's moved ControlObjectModel parentSourceItem = GetController(item.Parent_ID); // If the parent is in the root level if (parentSourceItem == null) { // Remove the item in the root level ControlObjects.Remove(item); } else { // Otherwise remove the item from the child collection parentSourceItem.ChildControlObjects.Remove(item); } TreeListViewDropPosition relativeDropPosition = (TreeListViewDropPosition)destination.GetValue(RadTreeListView.DropPositionProperty); destination.UpdateLayout(); // If put on top of destination if (relativeDropPosition == TreeListViewDropPosition.Inside) { // the Parent_ID of the item will become the ID of the destination item.Parent_ID = destinationItem.ID; destinationItem.ChildControlObjects.Add(item); } // If put before or after the destination else { // if the desitination is in the root collection if (destinationItem.Parent_ID == null) { // The parent_ID of the item will also be null item.Parent_ID = null; ControlObjects.Insert(ControlObjects.IndexOf(destinationItem), item); } else { // otherwise the Parent_ID of the item will be the same as that of the destination item item.Parent_ID = destinationItem.Parent_ID; // find the Parent of the destination item parentSourceItem = GetController(destinationItem.Parent_ID); // Insert the item before or after the destination item in the ChildObject collection of the parent of the destination if (relativeDropPosition == TreeListViewDropPosition.Before) { parentSourceItem.ChildControlObjects.Insert(parentSourceItem.ChildControlObjects.IndexOf(destinationItem), item); } else { parentSourceItem.ChildControlObjects.Insert(parentSourceItem.ChildControlObjects.IndexOf(destinationItem) + 1, item); } } } } } catch (Exception ex) { RadWindow.Alert(new DialogParameters() { Header = "Error", Content = "Error while moving object\n" + ex.Message }); } } }