Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ protected DragDropEffects DoDragDrop(DependencyObject dragSource)
{
Content = new ContentControl { Content = data, ContentTemplate = DragVisualTemplate }
};

// Set initial position to the cursor location to avoid the element potentially flashing at the system-default window position.
var rootSource = PresentationSource.FromVisual(AssociatedObject);
if (rootSource != null)
{
var cursorRelToSource = AssociatedObject.GetCursorRelativePosition();
var cursorScreenPhysical = AssociatedObject.PointToScreen(cursorRelToSource);
var cursorInDip = rootSource.CompositionTarget.TransformFromDevice.Transform(cursorScreenPhysical);
DragWindow.Left = cursorInDip.X + 16;
DragWindow.Top = cursorInDip.Y;
}

DragWindow.Show();

try
Expand Down Expand Up @@ -317,7 +329,10 @@ protected object InitializeDrag(object originalSource)
var itemsToDrag = GetItemsToDrag(container).ToList();
if (itemsToDrag.Count > 0)
{
var dragContainer = new DragContainer(itemsToDrag);
var dragContainer = new DragContainer(itemsToDrag)
{
Message = DragDropBehavior.InvalidDropAreaMessage
};
data = dragContainer;
}
return data;
Expand Down Expand Up @@ -502,25 +517,14 @@ private void PreviewMouseMove(object sender, [NotNull] MouseEventArgs e)

private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (DragWindow == null)
return;

// Get root window
var rootWindow = Window.GetWindow((DependencyObject)sender);
if (rootWindow == null)
if (DragWindow == null || PresentationSource.FromVisual(DragWindow) == null)
return;

// Get position in WPF screen coordinates
var point = rootWindow.GetCursorScreenPosition();
// Get the relative DPI between the two windows
var rootDpi = VisualTreeHelper.GetDpi(rootWindow);
var dragDpi = VisualTreeHelper.GetDpi(DragWindow);
// Calculate relative DPI scale
var dpiRatioX = rootDpi.DpiScaleX / dragDpi.DpiScaleX;
var dpiRatioY = rootDpi.DpiScaleY / dragDpi.DpiScaleY;
// Move drag window accordingly
DragWindow.Left = (point.X + 16) * dpiRatioX;
DragWindow.Top = point.Y * dpiRatioY;
// Get the cursor position in DragWindow's own coordinate space and adjust its position so the cursor sits just to the left of DragWindow.
// Using the DragWindow's own coordinate space allows the drag visual to be properly positioned across various DPI / monitor settings.
var cursorRelative = DragWindow.GetCursorRelativePosition();
DragWindow.Left += cursorRelative.X + 16;
DragWindow.Top += cursorRelative.Y;
}

private void OnDragOver(object sender, [NotNull] DragEventArgs e)
Expand Down