private void AdornerMouseUp(object sender, MouseEventArgs e) { switch (adorner.AdornerDropState) { case DropState.CanDrop: DropElement(); break; case DropState.CannotDrop: DoNotDropElement(); break; } lastDraggedElement = null; adornerLayer.PreviewMouseMove -= AdornerMouseMove; adornerLayer.PreviewMouseUp -= AdornerMouseUp; if (adorner != null) { adorner.ReleaseMouseCapture(); } adorner = null; mouseCaptured = false; }
// Drag = mouse down + move by a certain amount private void OnDragSourcePreviewMouseMove(object sender, MouseEventArgs e) { if (!mouseCaptured && lastDraggedElement != null) { // Only drag when user moved the mouse by a reasonable amount. if (IsMovementBigEnough(initialMousePosition, e.GetPosition(topWindow))) { adorner = (DragDropAdorner)GetDragDropControl(sender as DependencyObject); adorner.DataContext = lastDraggedElement.DataContext; adorner.Opacity = 0.7; adornerLayer.Visibility = Visibility.Visible; adornerLayer.Children.Add(adorner); mouseCaptured = Mouse.Capture(adorner); Canvas.SetLeft(adorner, initialMousePosition.X - 20); Canvas.SetTop(adorner, initialMousePosition.Y - 15); adornerLayer.PreviewMouseMove += AdornerMouseMove; adornerLayer.PreviewMouseUp += AdornerMouseUp; } } }
/// <summary> /// Make a <see cref="DependencyObject" /> draggable. /// </summary> /// <param name="dependencyObject">The object to make draggable</param> /// <param name="dropTargets">The drop targets names.</param> /// <param name="dragDropAdorner">The drag drop adorner implementation to use.</param> /// <param name="adornerLayerName">The adorner view layer.</param> public static void MakeObjectDraggable(DependencyObject dependencyObject, IEnumerable <string> dropTargets, DragDropAdorner dragDropAdorner, string adornerLayerName) { string targets = string.Join(",", dropTargets); dependencyObject.SetValue(DragDropHelper.IsDragSourceProperty, true); dependencyObject.SetValue(DragDropHelper.DropTargetsProperty, targets); dependencyObject.SetValue(DragDropHelper.AdornerLayerProperty, adornerLayerName); dependencyObject.SetValue(DragDropHelper.DragDropControlProperty, dragDropAdorner); }
private static void DropStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DragDropAdorner myclass = (DragDropAdorner)d; myclass.StateChangedHandler(d, e); }