Dropping a file in a WPF TextBox
Asked Answered
A

4

9

I have found this question MANY times on Google and Stack Overflow. None of the solutions work.

I have a TextBox. AllowDrop is set to true. I first tried DragEnter/DragOver/Drop events and then switched to "Preview" events for all of these. No event EVER gets called no matter what I do. Next I tried adding handlers after InitializeComponent(). No luck.

Xaml - commented out because I can't post it otherwise:

<TextBox PreviewDragEnter="OutputFolder_DragEnter" PreviewDragOver="OutputFolder_DragOver" AllowDrop="True" PreviewDrop="OutputFolder_Drop" />

No C# code posted because no breakpoint is ever hit. It simply doesn't work. As I mentioned, I did try adding a handler manually but still can't get it working.

Ambit answered 30/7, 2014 at 17:9 Comment(1)
Your exact code (as far as I can tell from your question) worked for me.Pudding
A
2

Are you running Visual Studio as an administrator when this problem happens? Windows/UAC will usually block drag/drop operations from outside the running program if it was launched with elevated privileges (especially if you're on a user account that isn't an administrator for the domain).

This is a problem I commonly run into which can bug for me far too long before I remember to restart Visual Studio as a non-administrator (N.B. this problem applies to both WPF and WinForms projects).

Al answered 15/6, 2016 at 20:36 Comment(0)
E
5

Subscribe the PreviewDragHandler and set e.Handled = true. Then Drop event should fire.

    private void TextBox_PreviewDragOver(object sender, DragEventArgs e)
    {
        e.Handled = true;
    }

XAML looks like below,

    <TextBox VerticalAlignment="Center" AllowDrop="True" 
PreviewDragOver="TextBox_PreviewDragOver"/>
Emyle answered 1/8, 2014 at 7:3 Comment(2)
From the OP - I already have the preview drag event assigned. It never hits it.Ambit
For what it's worth, I also had to subscribe to the PreviewDragEnter event, otherwise there was a moment when the dragging mouse pointer would switch its DragDropEffect to None. Subscribing to both, and setting e.Handled = true allowed me to handle all drop events from the MainWindow as I wanted. Thanks for the tip!Engstrom
A
5

7 years later...

I gave up on drag and drop at the time of this question. I could not get it working. I ended up needing it in a totally unrelated application and again couldn't get it working. In some Google searches I ended up on my own darn question here. But finally solved it!

Visual Studio is set to run as Administrator for me. This is required for IIS and web applications. But Windows Explorer will not allow a drag and drop file between my Explorer window and my application because they are running in different elevations/user rights. If I start the application stand alone it actually worked and probably worked 7 years ago too. Starting without debugging is not a solution because Visual Studio still starts it. Need to start by double clicking the EXE.

Ambit answered 9/11, 2021 at 18:56 Comment(0)
G
2

IIRC Drag Events only get raised if the drag started inside the WPF application. What you want is the Drop Event. This Code works fine for me.

C#:

private void ListBox_Drop(object sender, DragEventArgs e)
{
    var fileNames = (string[]) e.Data.GetData(DataFormats.FileDrop);
    if (fileNames == null) return;
    var fileName = fileNames.FirstOrDefault();
    if (fileName == null) return;
    (sender as ListBox).Items.Add(fileName);
}

xaml:

<ListBox AllowDrop="True" Drop="ListBox_Drop" />
Gynecology answered 1/8, 2014 at 7:37 Comment(6)
and no event got raised? Do you have a transparent Background somewhere?… I had problems caused by that a while ago.Gynecology
No transparent background. Just a window with a grid and a textbox in that. I assigned the preview events but they never get hit. Then I assigned handlers manually in code. Dragging a file from explorer into a text box. Only ever see the circle/slash meaning no drag.Ambit
Yeah… I just tried it with a TextBox too… didn’t thought the control makes a difference, but I was wrong. Apparently the TextBox blocks all drag/drop events. Maybe that’s caused by its ability to copy/past text via Drag&Drop…Gynecology
I have tried to write a DropTextBox (inherited from TextBox) and then I have overwritten the OnDrop method, but even that doesn’t get called. I somehow think somebody doesn’t want you to drop to a TextBox. Could you drop on an other control/items control or would that destroy your UI Design?Gynecology
It's just a text box showing a file path. There's a simple lookup button that kicks off an OpenDialog. and you can type manually. It would have been nice to allow drag and drop, and I figured Iwas just doing something wrong. Other posts all seem to be able to make it work using preview, but the event will not file for me. The AddHandler method takes a third parameter of boolean indicating if already handled events should fire again. Even this doesn't fire. Maybe it's not possible, but I find many instances of people saying it is.Ambit
Not sure if you ever got this figured out, but TextBox has some default handling for DragAndDrop, so you need to set e.Handled to true in the PreviewDragOver handler. See this question: #4282357Fourlegged
A
2

Are you running Visual Studio as an administrator when this problem happens? Windows/UAC will usually block drag/drop operations from outside the running program if it was launched with elevated privileges (especially if you're on a user account that isn't an administrator for the domain).

This is a problem I commonly run into which can bug for me far too long before I remember to restart Visual Studio as a non-administrator (N.B. this problem applies to both WPF and WinForms projects).

Al answered 15/6, 2016 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.