WPF
Drag and Drop
File Handling
User Interface
C# Development

Drag and drop files into WPF

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

WPF can accept files dragged from Windows Explorer with only a few properties and event handlers. The important pieces are enabling dropping on the target element, checking for the FileDrop data format, and handling user feedback so the operation feels intentional instead of accidental.

Basic WPF Setup

The drop target must allow drops and subscribe to the drag events you care about.

xml
1<Window x:Class="DragDropDemo.MainWindow"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Drag Drop Demo" Height="300" Width="400">
5    <Grid Margin="16">
6        <ListBox Name="FileList"
7                 AllowDrop="True"
8                 DragEnter="FileList_DragEnter"
9                 Drop="FileList_Drop" />
10    </Grid>
11</Window>

AllowDrop="True" is the switch that tells WPF the control can receive drop operations.

Reading the Dropped Files

When files are dragged from Explorer, the payload usually arrives as DataFormats.FileDrop.

csharp
1using System.IO;
2using System.Windows;
3
4namespace DragDropDemo;
5
6public partial class MainWindow : Window
7{
8    public MainWindow()
9    {
10        InitializeComponent();
11    }
12
13    private void FileList_DragEnter(object sender, DragEventArgs e)
14    {
15        if (!e.Data.GetDataPresent(DataFormats.FileDrop))
16        {
17            e.Effects = DragDropEffects.None;
18            e.Handled = true;
19            return;
20        }
21
22        e.Effects = DragDropEffects.Copy;
23        e.Handled = true;
24    }
25
26    private void FileList_Drop(object sender, DragEventArgs e)
27    {
28        if (!e.Data.GetDataPresent(DataFormats.FileDrop))
29            return;
30
31        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
32
33        foreach (string path in files)
34        {
35            FileList.Items.Add(Path.GetFileName(path));
36        }
37    }
38}

This example displays only file names, but you can just as easily store full paths, validate extensions, or start an import workflow.

Handling Folders and Validation

Explorer can also drop directories, so do not assume every path is a file. Check what was dropped before processing it.

csharp
1foreach (string path in files)
2{
3    if (Directory.Exists(path))
4    {
5        FileList.Items.Add($"[Folder] {Path.GetFileName(path)}");
6    }
7    else if (File.Exists(path))
8    {
9        FileList.Items.Add(Path.GetFileName(path));
10    }
11}

This is also the right place to reject unsupported types:

  • only accept .txt files
  • skip directories
  • show a message when the payload is invalid

Early validation makes drag and drop feel predictable.

Better User Feedback

The DragEnter event is enough for basic support, but richer apps often also handle DragOver and DragLeave to change visuals while the pointer hovers over the target.

For example, you might:

  • highlight the border
  • show a "Drop files here" message
  • switch the mouse effect between copy and none

Those details matter because drag and drop is a spatial interaction. If the target gives no feedback, users hesitate.

Binding-Friendly Approach

For production code, it is often cleaner to bind the ListBox to an ObservableCollection<string> rather than adding items directly in the code-behind.

csharp
1using System.Collections.ObjectModel;
2
3public ObservableCollection<string> Files { get; } = new();
4
5public MainWindow()
6{
7    InitializeComponent();
8    DataContext = this;
9}

Then the drop handler updates Files instead of the control directly. That keeps the UI easier to test and maintain.

Common Pitfalls

The most common mistake is forgetting AllowDrop="True". Without it, the event handlers may be wired up correctly but the control still refuses drops.

Another issue is assuming the payload always contains files. Explorer usually provides FileDrop, but real-world drag sources can use different formats, so check the data before casting.

A third pitfall is doing heavy file processing directly inside the drop event on the UI thread. If the drop triggers parsing or copying large files, hand the work off asynchronously and keep the interface responsive.

Finally, do not ignore folders if users may drag them in. Many desktop users expect folder drops to work just as naturally as file drops.

Summary

  • Enable dropping with AllowDrop="True" on the target control.
  • Check for DataFormats.FileDrop before reading dropped paths.
  • Validate whether each dropped path is a file or a directory.
  • Give users feedback through drag effects and hover styling.
  • Move heavy processing out of the UI thread once the basic drop flow works.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.