views:

3338

answers:

4

Is it possible to have a WPF window/element detect the drag'n'dropping of a file from windows explorer in C# .Net 3.5? I've found solutions for WinForms, but none for WPF.

A: 

http://stackoverflow.com/questions/316900/dragndrop-one-or-more-mails-from-outlook-to-c-wpf-application

Donnelle
Thanks for the link, but I already tried that one
NoizWaves
Didn't have more time to summarise but thought it might get you started. Glad to see you got it sorted.
Donnelle
+1  A: 

Turns out I couldn't drop onto my TextBox for some reason, but dropping onto buttons works fine. Got it working by adding 'AllowDrop="True"' to my window and adding drop event handler to button consisting of:

private void btnFindType_Drop(object sender, DragEventArgs e)
{
  if (e.Data is System.Windows.DataObject &&
    ((System.Windows.DataObject)e.Data).ContainsFileDropList())
  {
    foreach (string filePath in ((System.Windows.DataObject)e.Data).GetFileDropList())
    {
      // Processing here
    }
  }            
}
NoizWaves
+2  A: 

Unfortunately, TextBox, RichTextBox, and FlowDocument viewers always mark drag-and-drop events as handled, which prevents them from bubbling up to your handlers. You can restore drag-and-drop events being intercepted by these controls by force-handling the drag-and-drop events (use UIElement.AddHandler and set handledEventsToo to true) and setting e.Handled to false in your handler.

Ed Ball
Cheers! Your idea is also discussed here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a539c487-1dec-4935-b91b-c3ec252eb834
NoizWaves
+1  A: 

Hi,

I noticed that drag&drop in WPF is not as easy as it could be. So I wrote a short article about this topic: http://www.wpftutorial.net/DragAndDrop.html