Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF drag and drop and data types

How does one get the type of the dropped object? How can it be added it to a specific struct/list?

like image 933
geo Avatar asked Apr 20 '11 08:04

geo


2 Answers

Assuming you control the start of the drag (you're not dragging from another app), it's up to you what the type is. Just make the source and destination code match. In the drag (typically a MouseMove or MouseDown handler):

        var dragData = new DataObject(typeof(JobViewModel), job);
        DragDrop.DoDragDrop(element, dragData, DragDropEffects.Move);

Begins the drag. And then in the drop (it sounds like you've gotten this far):

        var dataObj = e.Data as DataObject;
        var dragged = dataObj.GetData(typeof(JobViewModel)) as JobViewModel;

You can also use a String instead of a Type.

like image 85
default.kramer Avatar answered Nov 03 '22 02:11

default.kramer


Just set the control's AllowDrop property to true. And implement the Drop event on it; you can access the drop information in the event argument.

For the GetData part, you can use this to get specific data types. Here is the file drop for example:

string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, true);

Thanks,

like image 1
Howard Avatar answered Nov 03 '22 03:11

Howard