private async void DoDeleteRequirement()
        {
            await DialogHost.Show(new OkCancelMessageDialog()
            {
                DataContext = $"Delete {SelectedRequirement.Name}?"
            },
                                  "RequirementDialog",
                                  delegate(object sender, DialogClosingEventArgs args)
            {
                bool result = false;
                if (Equals(args.Parameter, "Cancel"))
                {
                    return;
                }

                if (_isOkMessageOpen)
                {
                    _isOkMessageOpen = false;
                    return;
                }

                if (Equals(args.Parameter, "Ok"))
                {
                    args.Session.UpdateContent(new PleaseWaitView());
                    Task.Run(() =>
                    {
                        try
                        {
                            //_context.Entry(SelectedRequirement).State = EntityState.Deleted;
                            //var students = _context.Students.ToList();
                            //foreach (var student in students)
                            //{
                            //     student.RequirementStudents.FirstOrDefault(
                            //        c => c.RequirementId == SelectedRequirement.Id);
                            //}
                            //var entity = _context.Requirements.FirstOrDefault(c => c.Id == SelectedRequirement.Id);
                            //if (entity != null) _context.Requirements.Remove(entity);
                            using (var context = new MorenoContext())
                            {
                                var entity =
                                    context.Requirements.FirstOrDefault(c => c.Id == SelectedRequirement.Id);
                                if (entity != null)
                                {
                                    context.Requirements.Remove(entity);
                                }
                                context.SaveChanges();
                                result = true;
                            }
                            //_context.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);

                            result = false;
                        }
                    }).ContinueWith((t, _) =>
                    {
                        if (!result)
                        {
                            args.Cancel();
                            _isOkMessageOpen = true;
                            args.Session.UpdateContent(new OkMessageDialog()
                            {
                                DataContext = "Failed to Delete"
                            });
                        }
                        else
                        {
                            Requirements.Remove(SelectedRequirement);
                        }
                    }, null, TaskScheduler.FromCurrentSynchronizationContext());
                }
            });
        }
Beispiel #2
0
        public void MoveSelection(TreeListViewRow destination)
        {
            if (destination != null)
            {
                RequirementModel destinationItem = (destination.DataContext) as RequirementModel;
                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 objects
                    TD.ObservableItemCollection <RequirementModel> selectedItems = new TD.ObservableItemCollection <RequirementModel>();
                    foreach (RequirementModel item in SelectedItems)
                    {
                        selectedItems.Add(item);
                    }

                    foreach (RequirementModel item in selectedItems)
                    {
                        // find the original parent of the object that's moved
                        RequirementModel parentSourceItem = GetRequirement(item.Parent_ID);

                        // If the parent is in the root level
                        if (parentSourceItem == null)
                        {
                            // Remove the item in the root level
                            Requirements.Remove(item);
                        }
                        else
                        {
                            // Otherwise remove the item from the child collection
                            parentSourceItem.ChildRequirements.Remove(item);
                        }

                        TreeListViewDropPosition relativeDropPosition = (TreeListViewDropPosition)destination.GetValue(RadTreeListView.DropPositionProperty);

                        // 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.ChildRequirements.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;
                                Requirements.Insert(Requirements.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 = GetRequirement(destinationItem.Parent_ID);
                                // Insert the item above the destination item in the ChildObject collection of the parent of the destination
                                if (relativeDropPosition == TreeListViewDropPosition.Before)
                                {
                                    parentSourceItem.ChildRequirements.Insert(parentSourceItem.ChildRequirements.IndexOf(destinationItem), item);
                                }
                                else
                                {
                                    parentSourceItem.ChildRequirements.Insert(parentSourceItem.ChildRequirements.IndexOf(destinationItem) + 1, item);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    RadWindow.Alert(ex.Message);
                }
            }
        }