18

I know there have been tons of answers on this subject, but I still cannot get it to work at all. I've enabled AllowDrop on every control at every level of the application, and tried to catch DragEnter and Drop on every control to no avail. I can drag and drop items inside the application, but any time I try to bring something in from Windows Explorer or the desktop, etc. it gives me the No icon. Any ideas what I might be doing wrong?

Here's an example of what I'm doing. Still does not show the move cursor and won't hit the MainWindow_DragEnter function.

    namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.DragEnter += new DragEventHandler(MainWindow_DragEnter);
        }

        void MainWindow_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Move;
        }
    }
}
Seabass__
  • 871
  • 2
  • 7
  • 19

2 Answers2

33

I've solved my problem I believe. I was running Visual Studio as Administrator. When it launched my application, it didn't recognize drags from Explorer because Explorer was running in User mode. Hope this bonehead move helps someone else out.

Seabass__
  • 871
  • 2
  • 7
  • 19
  • 1
    This wasn't my problem, but I found out that [TextBox, RichTextBox, and FlowDocument](http://stackoverflow.com/a/336283/116047) all need [some tweaking](http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a539c487-1dec-4935-b91b-c3ec252eb834) to enable drag-n-drop. – Pakman May 14 '12 at 17:08
  • I was running VS as Administrator on Window 7 and getting the "No" sign, same of course goes for running the application independently as Administrator. Now to figure out how to get Drag and Drop working for privilege elevated applications... http://stackoverflow.com/questions/2833709/c-sharp-drag-drop-does-not-work-on-windows-7 – Llyle Jun 10 '12 at 10:51
13

AllowDrop only activates the possibility to use drag & drop. You have to handle it yourself.

As you have seen, Objects that have AllowDrop also have the events DragEnter, DragOver, DragLeave. You have to code what you want to happen at this moments. You can change cursor, accept dropped items, etc. But you have to do it your own. E.g. look for DragEventArgs.Effects.

I just opened a new WPF app, selected window, enabled AllowDrop, added event handler DragEnter and put in there: e.Effects = DragDropEffects.Move; Works fine for me.

Marks
  • 3,613
  • 5
  • 31
  • 46
  • I tried this and it had the same results. I put a breakpoint in the MainMindow_DragEnter function, but it never gets hit when I drag something from the Explorer window. The cursor is still the "No" sign. – Seabass__ May 21 '10 at 16:04
  • 5
    Thanks, your solution would have worked fine if I wasn't in Administrator mode in Visual Studio. – Seabass__ May 21 '10 at 20:19
  • Thanks Seabass__ i was despairing on this "Admin" issue! – germanSharper Nov 20 '13 at 10:19