Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF/WinForms DragDrop IDataObject

I was wondering why WPF defines a new and apparently identical version of IDataObject for its drag/drop system?

I have application code which uses the winforms IDataObject which I now need to interoperate with WPF drag/drop events. Would it be safe to simply write an adapter class which implements System.Windows.Forms.IDataObject but passes calls into the actual System.Windows.IDataObject provided by WPF?

Thanks Dan

like image 543
dan Avatar asked Jul 22 '26 06:07

dan


1 Answers

Scenario

I found this question when I was trying to debug this problem:

I was dropping into a WinForms application, where inside the Drop event handler, the object I received was a System.Windows.Forms.IDataObject

However, I was using a library to do the heavy lifting of the Drop event, and it expected a object of type System.Windows.IDataObject. I was not able to edit the library source code.

Problem

If I tried to simply cast between the types ...

// data is of type System.Windows.Forms.IDataObject
var newData = (System.Windows.IDataObject)data; // debugger exits function after this line of code

... the debugger would just exit the function at that line of code without error. None of the code after that line would execute.

Solution

I created a proxy method that I called inside my WinForms event handler. The constructor for System.Windows.DataObject will accept a System.Windows.Forms.DataObject.

public void DropEventProxy(System.Windows.Forms.IDataObject data)
{
    System.Windows.IDataObject newData = new System.Windows.DataObject(data);    
    LibraryDropEventHandler(newData);
}

public string LibraryDropEventHandler(System.Windows.IDataObject data);
like image 60
Walter Stabosz Avatar answered Jul 23 '26 21:07

Walter Stabosz