private void BindMenuItem() { LeftMenu.Items.Clear(); var dropTagets = new List<DropTarget>(); foreach (var node in MenuDataList) { AccordionItem item; if (node.IsTop) { //only Top node can accept drag drop var target = new DropTarget { Ghost = new MenuGroupHeaderContent() { DataContext = node} }; target.DragSourceDropped += target_DragSourceDropped; dropTagets.Add(target); item = new AccordionItem { Header = target }; } else { item = new AccordionItem() { Header = new MenuGroupHeaderContent() {DataContext = node} }; } item.Content = new MenuItemContent() { DataSource = node.MenuItems}; LeftMenu.Items.Add(item); } }
private void InitControl() { // add droptargets to wrappanel DropTarget dropTarget1 = new DropTarget() {Ghost = new DropTargetGhost(), ShowHover=false , Width = 100, Height = 100}; DropTarget dropTarget2 = new DropTarget() { Ghost = new DropTargetGhost(), ShowHover=false , Width = 100, Height = 100}; DropTarget dropTarget3 = new DropTarget() { Ghost = new DropTargetGhost(), ShowHover=false , Width = 100, Height = 100}; DropTarget dropTarget4 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100}; DropTarget dropTarget5 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100}; DropTarget dropTarget6 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100}; PanelDropTargets.Children.Add(dropTarget1); PanelDropTargets.Children.Add(dropTarget2); PanelDropTargets.Children.Add(dropTarget3); PanelDragSources.Children.Add(dropTarget4); PanelDragSources.Children.Add(dropTarget5); PanelDragSources.Children.Add(dropTarget6); // create list of droptargets to pass to the dragsources List<DropTarget> dropTargets = new List<DropTarget>() { dropTarget1, dropTarget2, dropTarget3, dropTarget4, dropTarget5, dropTarget6}; // add dragsources to wrappanel DragSource dragSource1 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "1" } }, Ghost = new DragSourceGhost(), ShowReturnToOriginalPositionAnimation = false, DropTargets = dropTargets }; DragSource dragSource2 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "2" } }, Ghost = new DragSourceGhost(), ShowReturnToOriginalPositionAnimation = false, DropTargets = dropTargets }; DragSource dragSource3 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "3" } }, Ghost = new DragSourceGhost(), ShowReturnToOriginalPositionAnimation = false, DropTargets = dropTargets }; // add dragsources as content to droptargets dropTarget4.Content = dragSource1; dropTarget5.Content = dragSource2; dropTarget6.Content = dragSource3; }
void DragBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.DraggingEnabled && this.dragging) { // 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(); 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) { // return dragsource to original parent ReturnToOriginalPosition(); // trigger public event in droptarget (for external use) dropTarget.TriggerDragSourceDropped(this); } // remove canvas InitialValues.ContainingLayoutPanel.Children.Remove(DummyOverlay); } } 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)); } } }
private void InitControls() { //Important //see:http://silverlightdragdrop.codeplex.com/Thread/View.aspx?ThreadId=72180 SL_Drag_Drop_BaseClasses.InitialValues.ContainingLayoutPanel = this.LayoutRoot; var target1 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; var target2 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; var target3 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; var target4 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; var target5 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; var target6 = new DropTarget() { Ghost = new DropTargetContentGhost(), Width = 100, Height = 50 }; PanelDropTargets.Children.Add(target1); PanelDropTargets.Children.Add(target2); PanelDropTargets.Children.Add(target3); PanelDragSources.Children.Add(target4); PanelDragSources.Children.Add(target5); PanelDragSources.Children.Add(target6); var targets = new List<DropTarget> { target1, target2, target3, target4, target5, target6 }; //create objects you want to drag... var dragSource1 = new DragSource { Content = new DragSourceContent { DataContext = new TextLabel { LabelText = "label1" } }, DropTargets = targets, Ghost = new DragSourceContentGhost(), DragHandleMode = DragSource.DragHandleModeType.FullDragSource, }; var dragSource2 = new DragSource { Content = new DragSourceContent { DataContext = new TextLabel { LabelText = "label2" } }, DropTargets = targets, Ghost = new DragSourceContentGhost(), DragHandleMode = DragSource.DragHandleModeType.FullDragSource }; var dragSource3 = new DragSource { Content = new DragSourceContent { DataContext = new TextLabel { LabelText = "label3" } }, DropTargets = targets, Ghost = new DragSourceContentGhost(), DragHandleMode = DragSource.DragHandleModeType.FullDragSource }; target1.Content = dragSource1; target2.Content = dragSource2; target3.Content = dragSource3; }
private void InitControl() { // add droptargets to wrappanel DropTarget dropTarget1 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100, RemoveElementDropBehaviour= RemoveElementDropBehaviour.Replace }; DropTarget dropTarget2 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100, RemoveElementDropBehaviour = RemoveElementDropBehaviour.Replace }; DropTarget dropTarget3 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100, RemoveElementDropBehaviour = RemoveElementDropBehaviour.Replace }; PanelDropTargets.Children.Add(dropTarget1); PanelDropTargets.Children.Add(dropTarget2); PanelDropTargets.Children.Add(dropTarget3); // create list of droptargets to pass to the dragsources List<DropTarget> dropTargets = new List<DropTarget>() { dropTarget1, dropTarget2, dropTarget3 }; // add dragsources to wrappanel DragSource dragSource1 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "1" } }, DropTargets = dropTargets }; DragSource dragSource2 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "2" } }, DropTargets = dropTargets }; DragSource dragSource3 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "3" } }, DropTargets = dropTargets }; // add dragsources to wrappanel PanelDragSources.Children.Add(dragSource1); PanelDragSources.Children.Add(dragSource2); PanelDragSources.Children.Add(dragSource3); }
/// <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); } }
private void InitControl() { // add droptargets to wrappanel // droptarget 1 has no visible ghost // if that is the case, we must add a width & height. Default = auto, // so if we leave it at that it will have 0 width/height DropTarget dropTarget1 = new DropTarget() { Ghost = new DropTargetGhost(), GhostVisibility = Visibility.Collapsed, Width = 100, Height = 100 }; DropTarget dropTarget2 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100}; DropTarget dropTarget3 = new DropTarget() { Ghost = new DropTargetGhost() , Width = 100, Height = 100}; PanelDropTargets.Children.Add(dropTarget1); PanelDropTargets.Children.Add(dropTarget2); PanelDropTargets.Children.Add(dropTarget3); // create list of droptargets to pass to the dragsources List<DropTarget> dropTargets = new List<DropTarget>() { dropTarget1, dropTarget2, dropTarget3 }; // add dragsources to wrappanel DragSource dragSource1 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "1" } }, Ghost = new DragSourceGhost(), DropTargets = dropTargets }; DragSource dragSource2 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "2" } }, //Ghost = new DragSourceGhost(), DropTargets = dropTargets }; List<DropTarget> dropTargetsThird = new List<DropTarget>() { dropTarget1, dropTarget2 }; // dragsource 3 cannot be dropped in droptarget 3 // dragsource 3 has no visible ghost. We can set width/height if needed, but in this case, // it will take the widht/height of the Content (= DragSource) DragSource dragSource3 = new DragSource() { Content = new DragSourceContent() { DataContext = new Dummy() { DummyText = "3" } }, Ghost = new DragSourceGhost(), GhostVisibility = Visibility.Collapsed, DropTargets = dropTargetsThird }; // add dragsources to wrappanel PanelDragSources.Children.Add(dragSource1); PanelDragSources.Children.Add(dragSource2); PanelDragSources.Children.Add(dragSource3); }