WPF: Drag and Drop Textbox for Windows Explorer files

Since this was such a popular post I thought I would share it again.

Technical babble...

There are a lot of places on the web that talk about this concept and it’s clear there are a few ways to accomplish the task.

Here is the code I recommend:

Given:
Your TextBox name is “TextBox1”

C#

Now, let’s look at each event and cover what is going on:

We need to add this to override the default PreviewDrag behavior. If we don’t, then the Drop Event will not fire.

This piece deals with the source being dragged over the textbox. The data is evaluated at this point, and the DragDropEffects are executed accordingly. Notice that the data is checked for the correct type.

MSDN breaks down the DragDropEffects here.

In the drop event, a few things occur:

  1. We grab the DataObject from “e”.
  2. We evaluate the DataObject to ensure it has a StringCollection using GetFileDropList.
  3. We grab the StringCollection.
  4. We enumerate through the collection…

View original post 19 more words

Leave a comment