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; }
internal void TriggerInternalDragSourceDropped(DragSource source) { // Fire the drop target entered event if (InternalDragSourceDropped != null) { this.InternalDragSourceDropped(this, new DropEventArgs(source)); } }
internal void TriggerDropTargetLeft(DragSource source) { // Fire the drop target left event if (DropTargetLeft != null) { this.DropTargetLeft(this, new DropEventArgs(source)); } }
internal void TriggerDragSourceDropped(DragSource source) { // double-check: remove all borders source.RemoveAllDropBorders(); // Fire the drop target entered event if (DragSourceDropped != null) { this.DragSourceDropped(this, new DropEventArgs(source)); } }
/// <summary> /// Method overrides OnApplyTemplate to add handlers / get references to control in the template /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); // all our controls are inside of a canvas control. Because of this, it doesn't // automatically resize. We need to make sure the parent control is resized properly // get the main control host MainControlHost = (Grid)this.GetTemplateChild("MainControlHost"); // get the ghost control host GhostContentControl = (Grid)this.GetTemplateChild("GhostContentControl"); // get the main content host MainContentControl = (Grid)this.GetTemplateChild("MainContentControl"); // add the content if (plstContent.Children.Count > 0) { DragSource tmp = (DragSource)plstContent.Children[0]; plstContent.Children.Remove(tmp); Content = tmp; //ResetContent(); } // add the ghost? if (GhostVisibility == Visibility.Visible) { if (Ghost != null) { GhostContentControl.Children.Clear(); GhostContentControl.Children.Add(Ghost); } } // get bounding border for hover-effects BoundingBorder = (Border)this.GetTemplateChild("BoundingBorder"); // add handler for droptargetentered DropTargetEntered += new DropEventHandler(DropTargetBase_DropTargetEntered); // add handler for droptargetleft DropTargetLeft += new DropEventHandler(DropTargetBase_DropTargetLeft); // add handler for dragsourcedropped //DragSourceDropped += new DropEventHandler(DropTargetBase_DragSourceDropped); InternalDragSourceDropped += new DropEventHandler(DropTargetBase_InternalDragSourceDropped); if (AllowPositionSave) { (Application.Current.RootVisual as FrameworkElement).SizeChanged += new SizeChangedEventHandler(DropTarget_SizeChanged); } }
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); } }
/// <summary> /// Constructor /// </summary> /// <param name="source"></param> public DropEventArgs(DragSource source) { DragSource = source; }
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); }
internal void TriggerDragSourceDropped(DragSource source) { // double-check: remove all borders source.RemoveAllDropBorders(); // Fire the drop target dropped event if (DragSourceDropped != null) { this.DragSourceDropped(this, new DropEventArgs(source)); } }