Search code examples
c#windowsdrag-and-dropwinui-3

WinUI 3 Desktop: Drag and drop file from Explorer to app window


I have the following simple WinUI 3 Desktop app running on Window 11. Dragging a file from the Explorer into the window does not work. Grid_DragOver() is not being called. I just get the red cursor symbol as if drop is not allowed: enter image description here What am I doing wrong? Do I have to activate some kind of capabilities?

Strangely it works if I drag a file from the Recent items in the Start section of the Explorer: enter image description here But all other places like the Desktop don't work.

MainWindow.xaml:

    <Grid AllowDrop="True" Drop="Grid_Drop" DragOver="Grid_DragOver" Background="LightGray">
        <TextBlock
            x:Name="FileNameTextBlock"
            Text="Drag a file here"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="20"
            FontWeight="Bold" />
    </Grid>
</Window>

MainWindow.xaml.cs:

 using Microsoft.UI.Xaml;
 using Microsoft.UI.Xaml.Controls;
 using Microsoft.UI.Xaml.Input;
 using System;
 using System.IO;
 using Windows.ApplicationModel.DataTransfer;
 using Windows.Storage;

namespace FileDragDropApp
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private async void Grid_Drop(object sender, DragEventArgs e)
        {
            // Check if the dropped data contains files
            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                // Get the file(s) from the DataPackage
                var items = await e.DataView.GetStorageItemsAsync();
                if (items.Count > 0)
                {
                    // Display the first file's name
                    var storageFile = items[0] as StorageFile;
                    if (storageFile != null)
                    {
                        FileNameTextBlock.Text = $"File: {storageFile.Name}";
                    }
                }
            }
        }

        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            // This is not being called
            e.AcceptedOperation = DataPackageOperation.Copy;
        }
    }
}

EDIT I also tested DragEnter. Same result.

<StackPanel AllowDrop="True"  DragEnter="Grid_DragEnter" Background="LightGray">
    <TextBlock
     Text="Drag a file here"
     HorizontalAlignment="Center"
     VerticalAlignment="Center"
     FontSize="20"/>
</StackPanel>
private void Grid_DragEnter(object sender, DragEventArgs e)
{
    Debug.WriteLine("Grid_DragEnter");
}

Grid_DragEnter is not being called. Clean restart. Nothing.

Tested on:

Edition Windows 11 Pro

Version 24H2

OS build 26100.2161

Experience Windows Feature Experience Pack 1000.26100.32.0

Microsoft.Windows.SDK.BuildTools 10.0.26100.1742

Microsoft.WindowsAppSDK 1.6.240923002


Solution

  • This seems to be a Windows configuration issue, not a WinUI/WinAppSDK issue. According to the comments in the question, the following seems to help:

    1. Enable EnableLUA. (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System)
    2. Restart Windows.

    Since EnableLUA is supposed to be enabled(true/1) by default, group policies or 3rd party apps might have disabled it.