Wt.N
Wt.N

Reputation: 1658

Enter or over events are not fired when drag and drop is done on UWP

I'm making a desktop app with UWP for Windows. I was making drag and drop features, but it doesn't work. When I drag a file, it keeps showing "Not allowed" symbol,🚫. Drag events and over events seem not to be fired. Any help is greatly appreciated. I'm not running Visual Studio 2022 as an admin.

Mainpage.xamc.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace XML2PDFConverter
{
    /// <summary>
    /// Starting from a blank page.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void BackgroundGrid_DragEnter(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
        }

        private async void BackgroundGrid_Drop(object sender, DragEventArgs e)
        {
            if (e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();
                var filePaths = items.Select(x => x.Path).ToList();
            }
        }

        private void BackgroundGrid_DragOver(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
        }
    }
}

MainPage.xaml

<Page
    x:Class="XMLToPDFConverter.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="XMLToPDFConverter"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid x:Name="BackgroundGrid" AllowDrop="True" DragEnter="BackgroundGrid_DragEnter" Drop="BackgroundGrid_Drop" DragOver="BackgroundGrid_DragOver" />
</Page>

Upvotes: 0

Views: 105

Answers (1)

FrozenAssassine
FrozenAssassine

Reputation: 1450

The problem is, that your grid does not have a background color. It is enought to set the background color to transparent and it will work as expected:

<Grid x:Name="BackgroundGrid" Background="Transparent" AllowDrop="True" DragEnter="BackgroundGrid_DragEnter" Drop="BackgroundGrid_Drop" DragOver="BackgroundGrid_DragOver" />

Upvotes: 2

Related Questions