Accept any drop target

Apr 27, 2009 at 5:37 PM
Love the project. I got the code and was working with it in 5 minutes. The first thing I wanted to adapt was the code behind nature of the code. Maybe I haven't gotten into the code enough, but I don't want to specify predefined drop targets, so I wrote the code below. In the CheckIfIAmInDropTarget and GetCorrectDropTarget methods of the DragSource I loop through any DropTarget in the given object and accept that as a valid DropTarget. Works very well.

Now to a question / feature request. Now that I can bind a listbox to a collection and define the itemtemplate as a DropTarget with a DragSource content, I can rearrange my content. Very nice, except for one thing. When I drag an item to another item's position it replaces (erases is actually what it does). How difficult would it be to have it move the content being replaced to the original droptarget where I took my dragsource from, i.e. I have a,b,c,d,e. I drag d to b's place. Can I have b automatically move to d's place before the drag?


public static IEnumerable<DropTarget> GetDropTargetsRecursively(DependencyObject root)
List<DropTarget> results = new List<DropTarget>();
if (root is DropTarget)
results.Add(root as DropTarget);
int childCount = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < childCount; i++)
DependencyObject dobj = VisualTreeHelper.GetChild(root, i);
return results;
Apr 28, 2009 at 8:31 AM
Hello jjrdk,

glad to hear you like the project :-)  As far as your request is concerned: when you drop dragsource1 on droptarget2 which already has dragsource2, dragsource2 should automatically be moved to droptarget1 as long as dragsource2 has droptarget1 as one of its valid droptargets.  You can see this behaviour on various places in the running example. 

If dragsource2 hasn't got droptarget1 as a valid droptarget, either the drop is disallowed or dragsource2 is replaced (removed, actually) - this behaviour can be set via the "RemoveElementDropBehaviour"-property on a droptarget.

If you're sure dragsource2 has droptarget1 as a valid droptarget, and dragsource2 still doesn't move to droptarget1, something else is going wrong - I'd be glad to have a look at some example project to help you out.
May 16, 2009 at 7:56 PM

Hi there,


new version has been committed to source control (you can get it via the source code tab) - probably most interesting to you will be a new property, AllowAnyDropTarget, which will be of some help I guess :-)