Bind a drag source to a listbox item

May 11, 2009 at 12:56 AM

How can I bind a dragsource to a listbox item?

May 11, 2009 at 8:33 AM


what exactly are you trying to do?  If you want to make a listboxitem draggable, put a dragsource in the datatemplate of your listbox.  If you want to make it draggable and dropable on other listboxitems, you should put a droptarget in the datatemplate of your listbox, with a dragsource as its child.  However, to make this work correctly, you will have to do a few other things:

1) the droptargets must be registered as valid droptargets for your dragsources, so you have to make sure you fill the droptarget-list-property of every dragsource with all the droptargets (one for each listboxitem).  In the next release, a new property, "AllowAnyDropTarget", will be added to the dragsource, making this a lot easier to implement :-)

2) you must catch the DragSourceDropped-event on droptarget to implement the switching-logic in your collection. 

Hope this helps! :-)


May 16, 2009 at 7:54 PM



I've just committed a new version to source control which may address you issue - you can get it via the "source code" tab.


Quite a few internal changes have been made, most notably one "breaking change": ShowGhost (bool) has bene replaced by GhostVisibility (Visibility).  Next to that, I've added an "AllowAnyDropTarget"-property, which will override any list of DropTargets to make sure a DragSource can be dropped anywhere.  Another change is some additional functionality: DragSource now has a DragHandleMode-property, defining how you can drag your dragsource around: by the handle, or just by clicking anywhere on the source.  Another one: I've added a DropMode-property, which determines the behaviour when a dragsource is dropped, and the drop is valid: by default, the dragsource is dropped on the droptarget.  If you set DropMode to "ReturnDragSource", the dragsource is returned to where it originated from.  This mode can be useful if you just want to execute some code on dropping (dragsourcedropped event on droptarget is triggered and can be handled if you add a handler to it), but you do not want the dragsource to be removed from the originating collection.


That's about it, hope this helps :-)  I'd say the "AllowAnyDropTarget"-property should solve your problem :-)