Ejemplo n.º 1
0
        /// <summary>
        /// Handles the dropping of a dragsource in this droptarget
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void DropTargetBase_InternalDragSourceDropped(object sender, DropEventArgs args)
        {
            if (ShowHover)
            {
                // after this, start the hover-out animation on the droptarget
                Animation.CreateDropTargetHoverOut(BoundingBorder).Begin();
            }

            // what if there are children?
            if (MainContentControl.Children.Count > 0)
            {
                // get the current child (which is a dragsource by definition)
                // and either switch it with the new child (if the parent of the new child
                // is a valid droptarget for the current child - so it must have rights to
                // be used as a droptarget to be able to make the switch!) or replace it

                DragSource currentChild = (DragSource)MainContentControl.Children[0];

                // if currentchild <> child you're dragging (else, we're just dropping our
                // dragsource onto its own parent (droptarget)
                if (currentChild != args.DragSource)
                {
                    // is the new childs' parent (parent of parent of parent) a droptarget?
                    Panel firstParent = (Panel)VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(args.DragSource));

                    if (firstParent != null)
                    {
                        // droptarget?
                        if (VisualTreeHelper.GetParent(firstParent) is DropTarget)
                        {
                            DropTarget newChildParentDropTarget = (DropTarget)VisualTreeHelper.GetParent(firstParent);
                            if (currentChild.DropTargets.Contains(newChildParentDropTarget) ||
                                currentChild.AllDropTargetsValid == true)    // check for valid droptarget, or check if all droptargets are valid
                            {
                                // point needed for animation of the current child
                                Point from = new Point();
                                from = args.DragSource.getCurrentPosition();

                                // reset position of dragsource, so control is on top of ghost, right
                                // before actually moving it.
                                args.DragSource.ResetMyPosition();

                                // remove from current parent
                                ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                                MainContentControl.Children.Clear();
                                MainContentControl.Children.Add(args.DragSource);

                                // move the current child, with or without an animation
                                if (currentChild.ShowSwitchReplaceAnimation)
                                {
                                    // animation, from the current position to the new position
                                    // current position = where the new child is now
                                    // new position = where the new child was

                                    // add the current child to its new position, then move it from the
                                    // "current position" to 0, 0 (the new position)
                                    newChildParentDropTarget.MainContentControl.Children.Add(currentChild);

                                    //currentChild.AnimateOnSwitch(from);

                                    Storyboard sb = currentChild.ReturnAnimateOnSwitch(from);

                                    EventHandler handler = null;
                                    handler = (send, arg) =>
                                    {
                                        sb.Completed -= handler;
                                        // trigger external dragsourcedropped-event
                                        TriggerDragSourceDropped(args.DragSource);
                                    };
                                    sb.Completed += handler;
                                    sb.Begin();
                                }
                                else
                                {
                                    // no animation
                                    newChildParentDropTarget.MainContentControl.Children.Add(currentChild);

                                    // trigger external dragsourcedropped-event
                                    TriggerDragSourceDropped(args.DragSource);
                                }
                            }
                            else
                            {
                                // parent of the new child isn't a VALID droptarget.  Depending on DropBehaviour, remove
                                // current child & set new one (replace-behaviour) or return the new child to
                                // its original position (disallow-behaviour)

                                if (RemoveElementDropBehaviour == RemoveElementDropBehaviour.Replace)
                                {
                                    // reset position of dragsource, so control is on top of ghost, right
                                    // before actually moving it.
                                    args.DragSource.ResetMyPosition();

                                    // remove from current parent
                                    ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                                    MainContentControl.Children.Clear();
                                    MainContentControl.Children.Add(args.DragSource);

                                    // trigger external dragsourcedropped-event
                                    TriggerDragSourceDropped(args.DragSource);
                                }
                                else
                                {
                                    // drop is disallowed, return dragsource to original position

                                    args.DragSource.ReturnToOriginalPosition();

                                    // trigger external dragsourcedropped-event
                                    TriggerDragSourceDropped(args.DragSource);
                                }
                            }
                        }
                        else
                        {
                            // parent of the new child isn't a droptarget.  Depending on DropBehaviour, remove
                            // current child & set new one (replace-behaviour) or return the new child to
                            // its original position (disallow-behaviour)

                            if (RemoveElementDropBehaviour == RemoveElementDropBehaviour.Replace)
                            {
                                // reset position of dragsource, so control is on top of ghost, right
                                // before actually moving it.
                                args.DragSource.ResetMyPosition();

                                // remove from current parent
                                ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                                MainContentControl.Children.Clear();
                                MainContentControl.Children.Add(args.DragSource);

                                // trigger external dragsourcedropped-event
                                TriggerDragSourceDropped(args.DragSource);
                            }
                            else
                            {
                                // drop is disallowed, return dragsource to original position

                                args.DragSource.ReturnToOriginalPosition();

                                // trigger external dragsourcedropped-event
                                TriggerDragSourceDropped(args.DragSource);
                            }
                        }
                    }
                    else
                    {
                        // reset position of dragsource, so control is on top of ghost, right
                        // before actually moving it.
                        args.DragSource.ResetMyPosition();

                        // remove from current parent
                        ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                        MainContentControl.Children.Clear();
                        MainContentControl.Children.Add(args.DragSource);

                        // trigger external dragsourcedropped-event
                        TriggerDragSourceDropped(args.DragSource);
                    }
                }
                else
                {
                    // reset position of dragsource, so control is on top of ghost, right
                    // before actually moving it.
                    args.DragSource.ResetMyPosition();

                    // trigger external dragsourcedropped-event
                    TriggerDragSourceDropped(args.DragSource);
                }
            }
            else
            {
                // reset position of dragsource, so control is on top of ghost, right
                // before actually moving it.
                args.DragSource.ResetMyPosition();

                // remove from current parent
                ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                MainContentControl.Children.Clear();
                MainContentControl.Children.Add(args.DragSource);

                ((DropTarget)VisualTreeHelper.GetParent((Panel)VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(args.DragSource)))).Content = null;
                this.Content = args.DragSource;

                // trigger external dragsourcedropped-event
                TriggerDragSourceDropped(args.DragSource);
            }
        }