Skip to content

DragDropPreview remains on screen when dropping outside application #511

@nirvash

Description

@nirvash

Description:
When dragging items from the application and dropping them outside (e.g., onto Explorer or browser), the DragDropPreview remains visible on screen. This appears to be caused by missing cleanup in external drop scenarios.
During external drag-drop operations, multiple exceptions are thrown (COMException and NotImplementedException), which might be interrupting the normal cleanup flow. These exceptions appear to be silently caught somewhere in the drag-drop operation but are preventing proper cleanup of visual elements.
While the issue can be worked around with a custom DragHandler that cleans up the preview in DragDropOperationFinished, the library should handle this scenario properly, including better exception handling during external drop operations.

Workaround:

public class CustomDragHandler : DefaultDragHandler
{
    public override void DragDropOperationFinished(DragDropEffects operationResult, IDragInfo dragInfo)
    {
        base.DragDropOperationFinished(operationResult, dragInfo);
        
        // Clean up DragDropPreview using reflection
        var dragDropType = typeof(GongSolutions.Wpf.DragDrop.DragDrop);
        var dragDropPreviewField = dragDropType.GetField("dragDropPreview", 
            BindingFlags.NonPublic | BindingFlags.Static);
        
        if (dragDropPreviewField != null)
        {
            var preview = dragDropPreviewField.GetValue(null);
            if (preview != null)
            {
                var isOpenProperty = preview.GetType().GetProperty("IsOpen");
                if (isOpenProperty != null)
                {
                    isOpenProperty.SetValue(preview, false);
                }
                dragDropPreviewField.SetValue(null, null);
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions