Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application that has a mini-file-explorer. I want to be able to drag files from various sources (maybe Explorer or maybe an email attachment in an email program) and drop them on my file-explorer control. The type of the file does not matter because the semantics of the operation are always the same: copy the file into the current directory being displayed by my mini-file-explorer control.

How do I do this? I have read loads of stuff on the internet, but they all begin with the handling of the mouse-drag that initiates the drag-and-drop. I don't have any control over this because this occurs in one of a number of other applications.

Any help would be greatly appreciated.

What I have tried:

I have tried setting Drop="True" on my target control and providing a handler for the Drop event, but the control simply displays a no-entry icon when I drag a file onto it and the event does not fire.
Posted
Updated 25-Nov-16 7:19am

1 solution

You'll need to handle the DragDrop.DragOver attached event[^] and set the Effects property on the DragEventArgs parameter to an appropriate value based on the available data formats.
C#
private void control_DragOver(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.None;

    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effects = DragDropEffects.Copy;
    }
}

private void control_Drop(object sender, DragEventArgs e)
{
    string[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as string[];
    if (droppedFiles != null && droppedFiles.Length != 0)
    {
        ...
    }
}

Drag and Drop Overview[^]

I haven't used it, but I've just found this WPF drag/drop library[^], which looks promising.
 
Share this answer
 
Comments
Patrick Skelton 26-Nov-16 4:34am    
Thank you for the information. I wouldn't have worked that out any time soon.

I still get a no-entry sign when I drag over the application. I have tried adding the event handlers to the window itself, but still it doesn't work, so I am obviously missing a key piece of the jigsaw somewhere.
Richard Deeming 28-Nov-16 7:16am    
Does the DragOver event handler get hit? If so, does the GetDataPresent call return true?
Patrick Skelton 28-Nov-16 7:50am    
No. I've added event handlers for DragOver and PreviewDragOver, to both the control and the containing window. None are ever hit.

I'll have to play with the library above, to try to work out what it is doing that I am not. I'd simply use it, but it seems like overkill for my simple requirement.
Richard Deeming 28-Nov-16 7:52am    
And you've definitely set AllowDrop[^] to true?
Patrick Skelton 28-Nov-16 7:55am    
Yes, again on both the control and the containing window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900