1
votes

I have a ListBox that implements drag & drop: it works for single items (selecting an item then dragging it on top of another will put it in the right spot), but when selecting multiple items (using shift or ctrl) I have not been able to make it work.

My main issue is I don't know how the DragDrop.DoDragDrop function handles batches of items. It works for single items (specify the FrameworkElement, then the data format and the data, finally the drag effects). But if I have multiple items how do I use DoDragDrop? Since I have to specify a format wouldn't it always expect a single instance of that format? I have several 'Entities' I want to drag and drop, how do I tell DragDrop.DoDragDrop to accept all that data in one operation?

It works perfectly when I am passing just one DataObject to the DoDragDrop method, but I need to find a way to pass multiple objects in one call.

So far I've tried creating multiple DataObjects and tried creating an array or list to add to DoDragDrop as its data but that doesn't work. I also tried looping DoDragDrop so it gets called once for every item I'm dragging and that didn't work either.

1

1 Answers

1
votes

I've managed to solve the issue. For the data I passed a list of objects that I needed to drag, and when handling the drop I would receive the data as a list then do what I needed to do.

The problem I was having was that I couldn't wrap my head around the concept of a "format" not belonging to just one data object. For the format I simply put

DragDrop.DoDragDrop((FrameworkElement)sender, new DataObject("System.Collections.Generic.List<object>", _dragSources.Select(x => x.DataContext).ToList()), DragDropEffects.Copy);

and everything worked out. Even in the MSDN documentation it is mentioned nowhere that the "format" part is really just a key. You can put literally anything and as long as it matches what you are using to get the data (in this case e.Data.GetData("System.Collections.Generic.List<object>") as List<object>;) then you're golden.