Detect Drag'n'Drop file in WPF?
Asked Answered
H

4

15

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.

Heterologous answered 2/12, 2008 at 2:32 Comment(0)
T
6

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.

Tavel answered 3/12, 2008 at 5:14 Comment(1)
Cheers! Your idea is also discussed here: social.msdn.microsoft.com/forums/en-US/wpf/thread/…Heterologous
R
9

Try the following :

    private void MessageTextBox_Drop(object sender, DragEventArgs e)
    {
        if (e.Data is DataObject && ((DataObject)e.Data).ContainsFileDropList())
        {
            foreach (string filePath in ((DataObject)e.Data).GetFileDropList())
            {
                // Processing here     
            }
        }
    }


    private void MessageTextBox_PreviewDragEnter(object sender, DragEventArgs e)
    {
        var dropPossible = e.Data != null && ((DataObject)e.Data).ContainsFileDropList();
        if (dropPossible)
        {
            e.Effects = DragDropEffects.Copy;
        }
    }

    private void MessageTextBox_PreviewDragOver(object sender, DragEventArgs e)
    {
        e.Handled = true;
    }
Rollo answered 22/4, 2011 at 12:14 Comment(0)
T
6

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.

Tavel answered 3/12, 2008 at 5:14 Comment(1)
Cheers! Your idea is also discussed here: social.msdn.microsoft.com/forums/en-US/wpf/thread/…Heterologous
H
3

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
    }
  }            
}
Heterologous answered 2/12, 2008 at 3:2 Comment(0)
G
0

I had similar Issue, The drop events and drag enter events were not fired. The issue was with the windows User Account Settings. Set it to least secure setting and try the same code it works.

Grassland answered 22/3, 2012 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.