Drag/drop from ListBoxDragDropTarget to PanelDragDropTarget

寵の児 提交于 2019-12-11 06:56:12

问题


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


回答1:


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.




回答2:


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
    }
}



回答3:


This is answered in the following post in silverlight forum

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

-Mohamed



来源:https://stackoverflow.com/questions/3772960/drag-drop-from-listboxdragdroptarget-to-paneldragdroptarget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!