The WPF Notepad Project: Drag-and-Drop and Publish (Premium)

With the WPF version of .NETpad now roughly functionally identical to the WinForms version, I’m starting to think about additional unique features. The two I’m most interested in are things that I never got to with the previous versions: Making .NETpad the default application for text files and supporting drag-and-drop.

I’m still investigating the first one, but it doesn’t work right now (and it won’t work in any of the WinForms versions either). Vaguely, this requires the app to be able to handle startup parameters so that you could run the app from the command line and pass it the name of a file to open. (Something like .NETpad C:\Users\paul\Desktop\textfile.txt.) This is curiously difficult in WPF, but I’m working on it.

I have, however, figured out drag-and-drop, by which I mean the ability to drag a text file onto the application and have it open that file. So let’s take a look at that, and at Visual Studio’s very limited WPF application publishing capabilities.
Drag-and-drop
While Microsoft documents WPF drag-and-drop on its own site, I found the start of a good solution for this action from Stack Overflow, a vital resource for any developer. The answer found there has us create textbox Drop and PreviewDragOver event handlers, the first of which reads the full path and name of the file being dropped onto the textbox and then displays that information in the textbox.

To get started, open MainWindow.xaml and locate and select the line of XAML defining TextBox1. Add an AllowDrop property to TextBox1:
AllowDrop="True"
Then, display the available event handlers in the Properties pane and double-click next to Drop to create TextBox1_Drop in MainWindow.xaml.cs. Then, repeat that process for PreviewDragOver so that you create an empty TextBox1_PreviewDragOver event handler in MainWindow.xaml.cs as well.

If you run the app now and try to drag a text (or other file) onto the textbox, the cursor will change to indicate that you cannot drop the file.

That AllowDrop property indicates that we wish to allow drag-and-drop, but we can add code to TextBox1_PreviewDragOver that will indicate that we are actually handling the event that occurs when we drag a file over the textbox. So add the following code to TextBox1_PreviewDragOver:
e.Handled = true;
If you run the app now, you’ll see that the mouse cursor changes into a more positive form when you drag a file over the textbox, indicating that we can drop it.

As for actually handling the drop part of the drag-and-drop operation, that’s obviously what TextBox1_Drop is for. The Stack Overflow solution just writes the name of the file your dragging onto the textbox, so we’ll start there. Add the following code to TextBox1_Drop:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
    string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
    if (files != null && files.Length > 0)
    {
         ((TextBox)sender...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC