/// <summary>
        /// Find the Object with the searchItemID. This is a recursive function which looks through all levels of the tree
        /// If the treeLevel is omitted, the search will start in the root items
        /// </summary>
        /// <param name="searchItemID"></param>
        /// <param name="treeLevel"></param>
        /// <returns></returns>
        private ControlObjectModel GetController(Guid?searchItemID, TD.ObservableItemCollection <ControlObjectModel> treeLevel = null)
        {
            // Select the root level if the treeLevel = null
            if (treeLevel == null)
            {
                treeLevel = ControlObjects;
            }
            foreach (var controllerItem in treeLevel)
            {
                // return the item if found on this level
                if (controllerItem.ID == searchItemID)
                {
                    return(controllerItem);
                }

                if (controllerItem.ChildControlObjects != null)
                {
                    // Recursively call the method to find the item in the ChildControlObjects
                    ControlObjectModel childcontrollerItem = GetController(searchItemID, controllerItem.ChildControlObjects);
                    if (childcontrollerItem != null)
                    {
                        return(childcontrollerItem);
                    }
                }
            }
            return(null);
        }
        public void Paste()
        {
            foreach (var controllerItem in CopiedItems)
            {
                ControlObjectModel copiedcontrollerItem = new ControlObjectModel();
                copiedcontrollerItem.ID                   = Guid.NewGuid();
                copiedcontrollerItem.Project_ID           = controllerItem.Project_ID;
                copiedcontrollerItem.ObjectName           = controllerItem.ObjectName + "_1";
                copiedcontrollerItem.Description          = controllerItem.Description;
                copiedcontrollerItem.ControlObjectType_ID = controllerItem.ControlObjectType_ID;
                copiedcontrollerItem.IsChanged            = false;
                copiedcontrollerItem.IsNew                = true;
                copiedcontrollerItem.ChildControlObjects  = new TD.ObservableItemCollection <ControlObjectModel>();

                if (SelectedItem == null)
                {
                    copiedcontrollerItem.Parent_ID = null;
                    ControlObjects.Add(copiedcontrollerItem);
                }
                // Otherwise get the parent object and add the new object as a child
                else
                {
                    copiedcontrollerItem.Parent_ID = SelectedItem.ID;
                    SelectedItem.ChildControlObjects.Add(copiedcontrollerItem);
                }
                //SelectedItem = copiedcontrollerItem;
            }

            IsChanged = true;
        }
        public void AddChild()
        {
            ControlObjectModel controllerItem = new ControlObjectModel
            {
                ID                  = Guid.NewGuid(),
                Project_ID          = Globals.Project_ID,
                ObjectName          = "New Object",
                Description         = "New Object Description",
                IsChanged           = false,
                IsNew               = true,
                ChildControlObjects = new TD.ObservableItemCollection <ControlObjectModel>()
            };

            if (SelectedItem != null)
            {
                controllerItem.Parent_ID            = SelectedItem.ID;
                controllerItem.ControlObjectType_ID = SelectedItem.ControlObjectType_ID + 1;
                SelectedItem.ChildControlObjects.Add(controllerItem);
            }
            IsChanged = true;
        }
        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>
        /// Loads the records from the DbSet into the ViewModel. This function designed for recursive use
        /// </summary>
        /// <param name="Project_ID"></param>
        /// <param name="Parent_ID"></param>
        /// <returns>Observable collection of VMControlObject</returns>
        private TD.ObservableItemCollection <ControlObjectModel> Load(Guid?Parent_ID)
        {
            TD.ObservableItemCollection <ControlObjectModel> ChildControlObjects = new TD.ObservableItemCollection <ControlObjectModel>();
            // TD.ObservableItemCollection<ControlObjectModel> personalLayout = new TD.ObservableItemCollection<ControlObjectModel>();

            using (EDBEntities eDB = new EDBEntities())
            {
                foreach (tblControlObject Rec in (from o in eDB.tblControlObjects where (o.Project_ID == Globals.Project_ID && o.Parent_ID == Parent_ID) orderby o.ObjectName select o))
                {
                    ControlObjectModel controllerItem = new ControlObjectModel
                    {
                        ID                   = Rec.ID,
                        Parent_ID            = Rec.Parent_ID,
                        Project_ID           = Rec.Project_ID,
                        ObjectName           = Rec.ObjectName,
                        Description          = Rec.Description,
                        ControlObjectType_ID = (int)Rec.ControlObjectType_ID,
                        IsChanged            = false,
                    };

                    // Load ControlObject with a parent_ID equal to the ID of this object
                    controllerItem.ChildControlObjects = Load(Rec.ID);

                    // If the parent ID is null, this is a root object and needs to be added to the collection that is the itemsource of the object tree
                    // Else it is a child object which needs to be added to the childobjectlist
                    if (Rec.Parent_ID == null)
                    {
                        ControlObjects.Add(controllerItem);
                    }
                    else
                    {
                        ChildControlObjects.Add(controllerItem);
                    }
                }
            }
            IsChanged = false;
            return(ChildControlObjects);
        }
        /// <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
                    });
                }
            }
        }