void DragBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.DraggingEnabled && this.dragging) { e.Handled = true; // Capture the mouse ((FrameworkElement)sender).ReleaseMouseCapture(); Point position = e.GetPosition(sender as UIElement); //CheckIfIAmInDropTarget(); // if I am in a droptarget, fire correct event, if not, return to original position if (isInDropTarget) { DropTarget dropTarget = GetCorrectDropTarget(e.GetPosition(this.MainDraggableControl)); //DropTarget dropTarget = GetCorrectDropTarget(); if (dropTarget != null) { //// put back on the correct parent DummyOverlay.Children.Remove(this); OriginalParent.Children.Add(this); if (DropMode == DropModeType.DropDragSource) { // trigger internal dragsourcedropped event to drop the dragsource // (after the animations etc, so after the drop has been executed, // the external DragSourceDropped event will be triggered) dropTarget.TriggerInternalDragSourceDropped(this); } else if (DropMode == DropModeType.ReturnDragSource || DropMode == DropModeType.ReturnDragSourceWithoutAnimation) { // return dragsource to original parent ReturnToOriginalPosition(DropMode == DropModeType.ReturnDragSourceWithoutAnimation); // trigger public event in droptarget (for external use) dropTarget.TriggerDragSourceDropped(this); } //// remove canvas InitialValues.ContainingLayoutPanel.Children.Remove(DummyOverlay); } else { ReturnToOriginalPosition(); } } else { ReturnToOriginalPosition(); } // Set dragging to false this.dragging = false; // Fire the drag finished event if (this.DragFinished != null) { this.DragFinished(this, new DragEventArgs(position.X - this.lastDragPosition.X, position.Y - this.lastDragPosition.Y, e)); } } }
/// <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 var currentChild = 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 = VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(args.DragSource)) as Panel; if (firstParent != null) { // droptarget? if (VisualTreeHelper.GetParent(firstParent) is DropTarget) { var currentDragSourceChild = currentChild as DragSource; DropTarget newChildParentDropTarget = (DropTarget)VisualTreeHelper.GetParent(firstParent); if (currentDragSourceChild != null && (currentDragSourceChild.DropTargets.Contains(newChildParentDropTarget) || currentDragSourceChild.AllDropTargetsValid)) // 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(); Point offsetCurrentChild = new Point(); if (currentDragSourceChild.ShowSwitchReplaceAnimation) { offsetCurrentChild = currentDragSourceChild.MainDraggableControl.TransformToVisual(InitialValues.ContainingLayoutPanel).Transform(new Point(0, 0)); } Point oriOffset = new Point(args.DragSource.OriginalOffset.X, args.DragSource.OriginalOffset.Y); // 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 (currentDragSourceChild.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 = currentDragSourceChild.ReturnAnimateOnSwitch(offsetCurrentChild, oriOffset); EventHandler handler = null; handler = (send, arg) => { sb.Completed -= handler; sb.Stop(); currentDragSourceChild.ResetMyPosition(); // 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); } }