开发者

How can I allow Drag and Drop from Windows Explorer into a C# WPF application?

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.DragE开发者_如何学编程nter += new DragEventHandler(MainWindow_DragEnter);
        }

        void MainWindow_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Move;
        }
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜