Site Logo

Pixel-in-Gene

Exploring Frontend Development with Design / Graphics / Technology

Improved DragDropManager - Source code

The DragDropManger has been a very handy class for me in couple of my projects. Since my last post I made some changes to the interfaces (IDragSourceAdvisor, IDropTargetAdvisor) and also to DragDropManager. There are no major changes except for a few method additions in the interfaces. These methods make it even more flexible.

Changes to IDragSourceAdvisor and IDropTargetAdvisor

Added method: UIElement GetTopContainer();

In my previous version I had assumed there would be only one MainWindow. Hence I was using Application.Current.MainWindow as my default top container. Of course this assumption worked well for some of my projects but broke when I had to do DnD across windows. This lead me to add the GetTopContainer() method, which is queried for the right container (AdornerDecorator).

How is this useful? Say for example you have a DragSource in one window and a DropTarget in another window. When an element in dragged onto the target window, the location of the feedback-UI is no longer relative to the MainWindow, instead it is relative to the target window. By querying the IDropTargetAdvisor for the top-container we can see the feedback-UI at the right place.

[Note] If you are using just the MainWindow, you can return Application.Current.MainWindow as your top container.

Added read-only property ApplyMouseOffset to IDropTargetAdvisor

When an element is moved from the source to the target, the initial click on the source-element may be at an offset inside its bounding-box. If the same offset needs to be applied to the feedback-UI on the target, the property ApplyMouseOffset can be set to True. In most cases you would set it as True. To accommodate a few outlier cases, this property had to be added.

That is all for the changes. I hope you find the code useful. If you do anything cool with it, I would love to know and shower praise on you ;)

Download DragDrop.zip

Improved DragDropManager - Source code
Pavan Podila
Pavan Podila
November 19th, 2007