2

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

dan
  • 123
  • 5

2 Answers2

1

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);
Community
  • 1
  • 1
Walter Stabosz
  • 7,447
  • 5
  • 43
  • 75
0

If it is for dragdrop inside of the same application, you can take a look at : WinForms Interop, Drag & Drop from WinForms -> WPF

Note that you will probably be better implemanting serialization for your object.

Community
  • 1
  • 1
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119