1

Using the Silverlight 4 toolkit, is it possible to drag and drop from a ListBox to a Canvas (and get an event on the drop into the Canvas)?

I was able to write code to drag/drop from a wrap panel inside a PanelDragDropTarget to a canvas but the event handler did not trigger when the drop occurred. When I tried to drag/drop from a ListBox inside a ListBoxDragDrop to the Canvas inside the PanelDragDropTarget, the drop did not occur (and the event handler didn't trigger). While dragging over the Canvas, the cursor turned into the one with up/down arrows.

Thanks, Ted

3 Answers 3

2

After the step described in the Mohamed's post there's still a little problem. When you hover over the target Panel element the drag-and-drop indicator looks like one for the scroll operation (and I needed the one with an arrow - for move operation). To ger rid of that effect I created a class derived from PanelDragDropTarget where I overrode two methods that caused the problem.

public class FixedPanelDragDropTarget : PanelDragDropTarget
{
    protected override bool CanAddItem(Panel itemsControl, object data)
    {
        return true;
    }

    protected override void InsertItem(Panel itemsControl, int index, object data)
    {
        //
    }

}

After that in the FixedPanelDragDropTarget.Drop event's handler we can manually create an intance of the dropped element, while the rest looks like it was supposed to.

1
  • Oh man, this is good stuff. I've struggled for hours to get a TreeViewDragDrop item to be accepted by my PanelDragDropTarget, your solution worked fine. Jun 15, 2012 at 9:29
1

This behavior occurs because the ListBoxDragDropTarget and the PanelDragDropTarget are written differently. When dragging between 2 ListBoxDragDropTargets you are transferring the piece of data that is bound to the control, whereas dragging between 2 PanelDragDropTargets transfers the UIElement that is "picked up."

This is why Dmitriyz' answer is required. The PanelDragDropTarget checks to see if the element you dragged from the ListBoxDragDropTarget is a UIElement, and since it is not, it's just a plain ol' piece of data, it returns false for CanAddItem. It disables the ability to drop. Therefore, you see the cursor with the up/down arrows.

To get everything to work, you must inherit from the PanelDragDropTarget and use that child class instead. It needs to override the CanAddItem method at a minimum:

public class ElementDragDropTarget : PanelDragDropTarget
{
    protected override bool CanAddItem(Panel itemsControl, object data)
    {
        return true;
    }

    protected override void InsertItem(Panel itemsControl, int index, object data)
    {
        // Do Work
    }
}
1
  • 1
    Oh man, this is good stuff. I've struggled for hours to get a TreeViewDragDrop item to be accepted by my PanelDragDropTarget, your solution worked fine. Jun 15, 2012 at 9:29
0

This is answered in the following post in silverlight forum

http://forums.silverlight.net/forums/p/140806/463877.aspx

-Mohamed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.