Dienstag, 15. Juni 2010

Drag and Drop files from WPF ListViews to the Desktop / Windows Explorer

Hey guys, this morning (yeah, beginning 7.00) I tried to implement what seemed to be quite an easy task: Dragging files out of my app onto the desktop or into an open explorer Window.

It’s not quite easy. So let’s start this small tutorial. First (of course) you create an ListView – let’s call it “FileView”.

<ListView x:Name="FileView" 
PreviewMouseLeftButtonDown="FileView_PreviewMouseLeftButtonDown"
MouseMove="FileView_MouseMove">
<ListView.View>
<GridView>
<!-- Put in here whatever you need -->
<GridViewColumn Header="Name" CellTemplate="{StaticResource file}" />
</GridView>
</ListView.View>
</ListView>


Now to the code behind the xaml. You might have noticed that I added two events to the ListView. The PreviewMouseLeftButtonDown (the dragging starts) and the MouseMove (the dragging is happening).



private Point start;

private void FileView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.start = e.GetPosition(null);
}

private void FileView_MouseMove(object sender, MouseEventArgs e)
{
Point mpos = e.GetPosition(null);
Vector diff = this.start - mpos;

if (e.LeftButton == MouseButtonState.Pressed &&
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
if (this.FileView.SelectedItems.Count == 0)
{
return;
}

// right about here you get the file urls of the selected items.
// should be quite easy, if not, ask.
//string[] files = ...;
string dataFormat = DataFormats.FileDrop;
DataObject dataObject = new DataObject(dataFormat, files);
DragDrop.DoDragDrop(this.FileView, dataObject, DragDropEffects.Copy);
}
}


Now that’s that, thanks for reading.

6 Kommentare:

  1. I have a question:

    Currently I am implementing a wpf app, I can drag the my file path to another textbox, now i want to add a feature to drag to desktop too.

    How can that be implement?

    Thanks.

    AntwortenLöschen
  2. Check out this post on stackoverflow:

    http://stackoverflow.com/questions/3040415/c-wpf-net-drag-and-drop-to-desktop-explorer

    AntwortenLöschen
  3. Hi ,
    thanks for the Code ,
    I have one issue with

    Can you please guide me for this line ? I didnt how to define "file" ?

    I have implemented the solution with


    but no luck.
    Please help me on this.

    AntwortenLöschen
  4. Hi,
    Thanks for this. One quick question. I'm writing a remote file viewer and want to drag-drop from the remote system to my local system. I have a chicken-egg scenario. I can download the files to temp files and use that for the input, but I don't want that to occur before they release the button over the target (i.e. Windows Explorer). If I do the copy in MouseMove, then the whole thing grinds to a halt while it downloads, before they do the actual drop. How do I do that?

    AntwortenLöschen
  5. How can I get Dropping location? If I drop a file in Desktop is there any way to get the Drop location in a Event as C:\Users\Sommnath\Desktop

    AntwortenLöschen
  6. Thank you so much for this! It helped a lot. Keep on coding. Big thanks from a coding bass guitar player.

    AntwortenLöschen

Socialize!