Paul
Paul

Reputation: 5924

Dropping a file in a WPF TextBox

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.

Upvotes: 9

Views: 6211

Answers (4)

Paul
Paul

Reputation: 5924

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.

Upvotes: 5

Dmihawk
Dmihawk

Reputation: 589

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).

Upvotes: 2

Marcel B
Marcel B

Reputation: 3664

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" />

Upvotes: 2

Jawahar
Jawahar

Reputation: 4885

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"/>

Upvotes: 5

Related Questions