PaperDiamond
PaperDiamond

Reputation: 3

Drag and Drop events not firing when trying to drag and drop files

This problem has been driving my crazy for a little while now, and I am not sure what the actual issue is. I am trying to add drag and drop functionality to one of my apps, specifically, dragging and dropping files from windows explorer into the app in order to bypass using the FileOpenPicker.

I have watched an MSDN video on Drag and Drop (https://channel9.msdn.com/Series/Windows-10-development-for-absolute-beginners/UWP-053-UWP-SoundBoard-Adding-Drag-and-Drop) as well as thoroughly read the documentation (https://learn.microsoft.com/en-us/windows/uwp/design/input/drag-and-drop).

I have created the most basic example below.

XAML:

<Grid AllowDrop="True"
          CanDrag="True"
          DragEnter="Grid_DragEnter"
          DragLeave="Grid_DragLeave"
          DragOver="Grid_DragOver"
          Drop="Grid_Drop"
          DropCompleted="Grid_DropCompleted"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">

    </Grid>

Code

private void Grid_DragEnter(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragEnter");
        }

        private void Grid_DragLeave(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragLeave");
        }

        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragOver");
        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            Debug.WriteLine("Drop");
        }

        private void Grid_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            Debug.WriteLine("DropCompleted");
        }

The issue is that when dragging single or multiple files into the app, not a single event ever fires. However, dragging and dropping UIElements from within the does cause the events to fire. I have also added the broadFileSystemAccess capability into the Appxmanifest which did not change the behavior.

So, how do I add drag and drop capabilities of files from Windows Explorer into my app? Is there some declaration that I am missing?

Upvotes: 0

Views: 1197

Answers (1)

sneusse
sneusse

Reputation: 1691

  1. If you run VS or your App as Administrator Drag&Drop will not work (for security reasons, see here https://superuser.com/questions/59051/drag-and-drop-file-into-application-under-run-as-administrator

  2. As long as your Grid does not contain anything it will not work and your Window will get the event. Try to set the Background to black or whatever color you like and your code should work.

Upvotes: 0

Related Questions