How to implement Drag & Drop Functionality in Universal Windows Platform (UWP)?

In the software lifecycle, drag and drop is a very useful, common, and impressive feature.
From the user perspective also, it is handy to use drag and drop instead of traditional open file dialogue box.
In UWP, it’s easy to implement and below, I’ve mentioned the step by step process to implement it.
Here are the steps:
  1. In Visual Studio, create New Project as “Blank App (Universal Windows)”.

  2. In MainPage.xaml, create a grid with the two columns, having identical width.
    1. <Grid.ColumnDefinitions>  
    2. <ColumnDefinition Width="*"/>  
    3. <ColumnDefinition Width="*"/>  
    4. </Grid.ColumnDefinitions>  
  3. Define a Grid, with these three important properties/events. 

    “AllowDrop”, “Drop” and “DragOver”.
    • AllowDrop is a Boolean property, which indicates whether dropping of the item on this control is allowed or not.
    • DragOver is an event, where we will show the user a caption, and glyphs of dragged item.
    • Drop event is useful, where we will define all the logic related to reading the file and displaying it (in our case).
    1. <Grid Grid.Row="0" Background="Aqua" AllowDrop="True"  
    2. Drop="Grid_Drop"  
    3. DragOver="Grid_DragOver">  
  4. Define an Image control, where we will show the dropped image from the folder.
    1. <Image Name="imgMain" Grid.Column="1"/>  
    Here, we’ve completed our Layout page. Now, moving to code at the backend file MainPage.xaml.cs.

  5. In MainPage.xaml.cs, inside DragOver event handler, write the code, given below:

    1. private void Grid_DragOver(object sender, DragEventArgs e)  
    2. {  
    3.     //To specifies which operations are allowed  
    4.     e.AcceptedOperation = DataPackageOperation.Copy;  
    5.     // To display the data which is dragged  
    6.     e.DragUIOverride.Caption = "drop here to view image";  
    7.     e.DragUIOverride.IsGlyphVisible = true;  
    8.     e.DragUIOverride.IsContentVisible = true;  
    9.     e.DragUIOverride.IsCaptionVisible = true;  
    10. }  
  6. Inside Drop event handler, write the code, given below:
    1. private void Grid_Drop(object sender, DragEventArgs e)  
    2. {  
    3.     if (e.DataView.Contains(StandardDataFormats.StorageItems))  
    4.     {  
    5.         var items = await e.DataView.GetStorageItemsAsync();  
    6.         if (items.Any())  
    7.         {  
    8.             var storageFile = items[0] as StorageFile;  
    9.             var contentType = storageFile.ContentType;  
    10.             StorageFolder folder = ApplicationData.Current.LocalFolder;  
    11.             if (contentType == "image/jpg" || contentType == "image/png" || contentType == "image/jpeg")  
    12.             {  
    13.                  StorageFile newFile = await storageFile.CopyAsync(folder, storageFile.Name, NameCollisionOption.GenerateUniqueName);  
    14.                 var bitmapImg = new BitmapImage();  
    15.                 bitmapImg.SetSource(await storageFile.OpenAsync(FileAccessMode.Read));  
    16.                 imgMain.Source = bitmapImg;  
    17.             }  
    18.         }  
    19.     }  
    20. }  
That’s all. Now run the Application.
P.S.: These drag and drop files are copied to the Application’s localState folder. Thus, you need to take care of the deletion of these files also.
You can find this folder at the location, given below:

Up Next
    Ebook Download
    View all
    View all