/// <summary> /// Tunneled event handler for when the mouse is moving /// </summary> /// <param name="sender">The object which invokes this event</param> /// <param name="e">Event args from the sender</param> private void DragSource_PreviewMouseMove(Object sender, MouseEventArgs e) { if (_mouseCaptured || _dragDropPreviewControlDataContext == null) { return; //we're already capturing the mouse, or we don't have a data context for the preview control } if (DragDrop.IsMovementBigEnough(_initialMousePosition, e.GetPosition(_topWindow)) == false) { return; //only drag when the user moved the mouse by a reasonable amount } _dragDropPreviewControl = (DragDropPreviewBase)GetDragDropPreviewControl(sender as DependencyObject); _dragDropPreviewControl.DataContext = _dragDropPreviewControlDataContext; _dragDropPreviewControl.Opacity = 0.7; _dragDropContainer.Children.Add(_dragDropPreviewControl); _mouseCaptured = Mouse.Capture(_dragDropPreviewControl); //have the preview control recieve and be able to handle mouse events //offset it just a bit so it looks like it's underneath the mouse Mouse.OverrideCursor = Cursors.Hand; Canvas.SetLeft(_dragDropPreviewControl, _initialMousePosition.X - 20); Canvas.SetTop(_dragDropPreviewControl, _initialMousePosition.Y - 15); _dragDropContainer.PreviewMouseMove += DragDropContainer_PreviewMouseMove; _dragDropContainer.PreviewMouseUp += DragDropContainer_PreviewMouseUp; }
/// <summary> /// Removes the drag and drop preview control from the drag drop canvas, and clears the reference for the next drag/drop operation /// </summary> private void FinalizePreviewControlMouseUp() { _dragDropContainer.Children.Remove(_dragDropPreviewControl); _dragDropContainer.PreviewMouseMove -= DragDropContainer_PreviewMouseMove; _dragDropContainer.PreviewMouseUp -= DragDropContainer_PreviewMouseUp; if (_dragDropPreviewControl != null) { _dragDropPreviewControl.ReleaseMouseCapture(); } _dragDropPreviewControl = null; Mouse.OverrideCursor = null; }
/// <summary> /// Sets the attached value of <see cref="DragDropPreviewControlProperty"/> /// </summary> /// <param name="element">The <see cref="DependencyObject"/> that the attached <see cref="DependencyProperty"/>, <see cref="DragDropPreviewControlProperty"/>, is attched to.</param> /// <param name="value">the value to set</param> public static void SetDragDropPreviewControl(DependencyObject element, DragDropPreviewBase value) { element.SetValue(DragDropPreviewControlProperty, value); }