Site Logo


Exploring Frontend Development with Design / Graphics / Technology

Drag & Drop with attached properties - Part 3

In my previous blog posts (Part 1, Part 2) on this topic, I have discussed how you can add drag-drop behavior to your application using attached properties. This greatly simplifies the event hook-up, keeps the XAML clean and literally gets rid of code-behind. However the previous version did not support the scenario where you are performing DragDrop across windows or across applications. This was pointed out in the comments to my earlier post (Part 2). Well now I am glad to say that even this scenario is supported.

With a change

To make this work I had to change the interfaces IDragSourceAdvisor and IDropTargetAdvisor slightly. In the previous implementation the IDragSourceAdvisor was requested to provide the preview feedback. This works great when all your DnD is happening in the same application window but it breaks when you are dragging across windows/apps.

Note that the preview feedback is shown inside the PreviewDragOver event handler, which is raised when the mouse is hovering over a potential drop target. However from DnD point of view the source should not have any knowledge of the possible drop targets. Thus when showing the feedback we should be requesting the target rather than the source for providing the visual. The target can use the DataObject to create a visual feedback specific to the content it is hosting. This is the change that I have introduced in the interfaces. The method GetVisualFeedback() has been moved from the IDragSourceAdvisor to IDropTargetAdvisor. It has the following signature:

public UIElement GetVisualFeedback(IDataObject obj)

A new sample

You can try running the sample examples either with one instance or with multiple instances. For example you can now drag a button from one window/app and drop in another instance of that window/app. There is also a new sample called CrossApp, where you drag and drop Text from an external application. The external application does not have to be a WPF application!


Drag & Drop with attached properties - Part 3
Pavan Podila
Pavan Podila
December 6th, 2006