Drag and Drop Between WPF Add-ins

Apr 30, 2009 at 2:51 PM
Does anyone know if drag and drop is supported between WPF add-ins? I've been trying to get this to work but I see the following exception when the mouse moves over the target WPF add-in:

System.Runtime.Serialization.SerializationException occurred
  Message="Type 'System.Windows.DataObject' in Assembly 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
       at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)

The source WPF add-in initiates the drag and drop operation as follows:

DragDrop.DoDragDrop(this, new DataObject("MyFormat", guidValue), DragDropEffects.Copy);

Where guidValue is an instance of type System.Guid. The target WPF add-in has AllowDrop set to true. This works fine in a normal application with 2 WPF controls but when the controls are add-ins I get the above exception and none of the OnDragXXX methods of the target WPF add-in are called. Have I missed something or does drag and drop simply not work between WPF add-ins?

May 27, 2009 at 12:25 PM

Well it looks like this is unsupported either as a result of how System.Windows.DataObject has been implemented or how the WPF add-in support has been added. The situation is not helped by the fact that you can't get away from using the WPF data object implementation when calling DragDrop.DoDragDrop, even if you pass in an implementation of the COM IDataObject interface. I've been able to get this to work by using the unmanaged APIs directly, which means you have to implement IDragSource (not too bad) and IDataObject (OMG!). Additionally, you cannot create instances of either the GiveFeedbackEventArgs or the QueryContinueDragEventArgs. This means you won't be able to use the normal drag-drop event interaction on the source control with your implementation of IDragSource unless you resort to reflection to invoke the internal constructors. So while you can get something working with the current framework I'm hoping they'll improve support for drag-drop operations in .NET 4.0.