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!

Download

Similar Posts:

6 responses to “Drag & Drop with attached properties – Part 3”

  1. (no name)
    How easy would it be (and what would I have to do) in order to drag a basic UIElement control onto a FlowDocument (ContentElement)?  I would of course wrap the UIElement control inside of a BlockUIContainer or an InlineUIContainer.  I’ve gone through and changed references to the TargetUI element to be a ContentElement rather than a UIElement.  However when I try to drag my UIElement on to the FlowDocument (hosted in a RichTextBox) I never get the cursor change that indicates I can drop the control there.  Any thoughts?
  2. (no name)

    It never fails. The minute I post something… =)

    I discovered I have to change how to "Find the Button" in the Advisor class

    public void OnDropCompleted(IDataObject obj, Point dropPoint)

    {

    Button b = Application.Current.MainWindow.FindName("dropButton") as Button;

  3. (no name)
    Appreciate the sample code, Pavan. One thing I noticed is that I get a ’NullReferenceException was unhandled"/"Object reference not set to an instance of an object" when I try to use this code on a Page rather than a Window. For example, I changed the CrossApp.xaml from a Window to a Page. Can you provide any suggestions on what might be causing this issue? Much Thanks.
  4. Lester
    nice code pavan. i fixed some of the glitches and posted it here: http://blogs.msdn.com/llobo/archive/2006/12/08/drag-drop-library.aspx
    kudos to u :)
  5. Pavan Podila

    Yes I am aware of that issue. I am still trying to figure how I can get the offset inside the dragged element. I didn’t want to hold up this blog post for that :)

  6. (no name)
    works nicely. One thing I noticed is that once the object is dragged the adorners (0,0) starts frm the mouse location. if I drag a rectangle from its center the adorner should also be dragged from its center.

Leave a Reply