Implementing File Drag & Drop In Your Windows 10 UWP Application

Drag & drop is a common feature in the Windows OS, mainly when you're moving a file from one place to another. Did you know that you can drag files into your app though? Well it's possible and a great way to improve the user experience of your application when it is running on a desktop or tablet PC where you might want to upload or insert a file into a view. 
 
 
The way this is done in your code is through a Boolean property and events on XAML controls which inherit from a UIElement. The Boolean property, AllowDrop, must be set to true on the control you want to enable dropping files onto. Also, the events for supporting drag & drop are DragEnter, DragLeave, DragOver and Drop. The two main events you want to handle are DragOver so that you can turn on the support for drag & drop and drop so you can get access to the collection of files that get dropped onto the control.
 
Your XAML will look something like the following,
  1. <Grid AllowDrop="True" DragOver="OnFileDragOver" DragLeave="OnFileDragLeave" Drop="OnFileDrop">  
  2.     <GridView x:Name="FileList" ItemsSource="{Binding Files}" IsItemClickEnabled="True" SelectionMode="None" />  
  3.   
  4.     <RelativePanel x:Name="AddFilePanel" Background="#44000000" Visibility="Collapsed">  
  5.         <SymbolIcon x:Name="AddSymbol" Symbol="Add" RelativePanel.AlignVerticalCenterWithPanel="True" RelativePanel.AlignHorizontalCenterWithPanel="True" Foreground="White" RenderTransformOrigin="0.5,0.5">  
  6.             <SymbolIcon.RenderTransform>  
  7.                 <CompositeTransform ScaleX="1.5" ScaleY="1.5"/>  
  8.             </SymbolIcon.RenderTransform>  
  9.         </SymbolIcon>  
  10.   
  11.         <TextBlock RelativePanel.Below="AddSymbol" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="0,12,0,0" Text="Add file to view" Foreground="White" FontSize="24"></TextBlock>  
  12.     </RelativePanel>  
  13. </Grid>  
And the code behind will look like the following:
  1. namespace DragDropXAML  
  2. {  
  3.     public class DragDropXAMLExample  
  4.     {  
  5.         public ObservableCollection<AppFile> Files { get; }  
  6.   
  7.         private void OnFileDragOver(object sender, DragEventArgs e)  
  8.         {  
  9.             e.AcceptedOperation = DataPackageOperation.Copy;  
  10.   
  11.             if (e.DragUIOverride != null)  
  12.             {  
  13.                 e.DragUIOverride.Caption = "Add file";  
  14.                 e.DragUIOverride.IsContentVisible = true;  
  15.             }  
  16.   
  17.             this.AddFilePanel.Visibility = Visibility.Visible;  
  18.         }  
  19.   
  20.         private void OnFileDragLeave(object sender, DragEventArgs e)  
  21.         {  
  22.             this.AddFilePanel.Visibility = Visibility.Collapsed;  
  23.         }  
  24.   
  25.         private async void OnFileDrop(object sender, DragEventArgs e)  
  26.         {  
  27.             if (e.DataView.Contains(StandardDataFormats.StorageItems))  
  28.             {  
  29.                 var items = await e.DataView.GetStorageItemsAsync();  
  30.                 if (items.Count > 0)  
  31.                 {  
  32.                     foreach (var appFile in items.OfType<StorageFile>().Select(storageFile => new AppFile { Name = storageFile.Name, File = storageFile }))  
  33.                     {  
  34.                         this.Files.Add(appFile);  
  35.                     }  
  36.                 }  
  37.             }  
  38.   
  39.             this.AddFilePanel.Visibility = Visibility.Collapsed;  
  40.         }  
  41.     }  
  42. }  
So now you know what to do, but how does the code work? We'll start with the OnFileDragOver method.
 
When you drag a file over your control, you need to modify the DragEventArgs to let it know what kind of method you're wanting from the drag & drop function, in this instance, we're copying the file. You can also choose to Move the file which will copy it and then delete the original, or you can Link it.

You also have the choice here to modify the UI which shows when you drag a file over your control. In this instance, we modify the caption and show the content as we drag it over.

As well as handling the event, I've also added an element to the UI which overlays the control with a hint to the user that shows them they can add files to the view while they drag over. We show this element when we are dragging and hide it when we leave or drop the files. I'd recommend doing something similar in your own application as this is a great addition to the drag & drop user experience.
The OnFileDrop method is what handles your files that you drop onto the control. In this instance, we're looking for StorageItems contained within the DataView property of the DragEventArgs and are specifically taking all of the StorageFiles and adding them to a GridView. It's at this point that you can manipulate your files in anyway you wish, for example, filtering out the specific files you're looking for by extension or opening the files into your app.
If you want to get a hold of the source I use for this, you can grab it on my GitHub here.
Got any questions? Leave them below and I'll get back in touch.


Similar Articles