Copy but don't move; dynamic targets

Jun 15, 2009 at 8:04 PM

Thank you very much for making such a terrific and useful component open and free to use. I'm using this as the basis for my first Silverlight project, and so far it's been tremendously helpful. Again, thank you.

I have a couple of questions, however...
1) I have two lists; one list I'll call the "source pool" and the other I'll call the "target pool". What I'm attempting is that when an item is dragged from the source pool and dropped in the target pool it is not removed from the source. The animations, ghosts, etc. can stay (and are preferable), but I'd like for the item from the source pool to essentially "copy" into the target pool, not completely move over (and thus be removed from the source pool list).

2) At run-time I'm dynamically adding droptargets to the target pool. For some reason, when I attempt to drop an item into one of the bottom targets in the target pool it moves to the one above it. I'm thinking maybe I'm having an indexing issue (see below)?
2a) When the page first loads it goes out and retrieves a list of items from a database. We'll call these Containers; a list of Containers. It dynamically populates an Accordion control with this list, and within each AccordionItem it's creating a stack panel that will hold drop targets.


void dataService_GetContainersCompleted(object sender, SilverlightPipelineConfiguration.DataSource.GetContainersCompletedEventArgs e)
AccordionContainer.Loaded += new RoutedEventHandler(AccordionContainer_Loaded);
            Collection allContainers = e.Result;
            for (int i = 0; i < allContainers.Count; i++)
                AccordionItem item = new AccordionItem();
                StackPanel sp = new StackPanel();
                sp.Height = 250;
                sp.Name = "StackPanel" + i;
                item.Content = sp;


2b) For each container I'm adding a single drop target into the stack panel I created previously once the accordion control has finished populating (shown previously):
void AccordionContainer_Loaded(object sender, RoutedEventArgs e)
            for (int i = 0; i < AccordionContainer.Items.Count; i++)
                AccordionItem item = AccordionContainer.ItemContainerGenerator.ContainerFromIndex(i) as AccordionItem;
                if (item != null)
                    DropTarget dt = new DropTarget()
                        Ghost = new DropTargetGhost()

                    object itemNode = item.FindName("StackPanel" + i);
                    if (itemNode is StackPanel)
                        StackPanel itemStack = itemNode as StackPanel;
But when I attempt to move an item from the source pool to one of the targets it's automatically trying to put it into the target before it. Very strange.
I'm hoping you can give me some pointers on how I might accomplish what I'm after, and I'd love to share with the rest of the community.
Thank you kindly in advance!



Jun 16, 2009 at 11:38 AM



as far as your first question is concerned: this behaviour is supported but not yet "officially" released.  You can, however, get it if you download the latest sourcecode :-)  A new property has been added, DropMode: when using this in mode "ReturnDragSource", items do not get removed from their current collection/position, but return; to get the behaviour you want, set that property to ReturnDragSource, and show return animation to false.  Now you can easily catch the "dragsourcedropped"-event on your droptarget to do whatever you need to do. :-)


As far as your second question is concerned, it's pretty hard to see what's going on from just looking at your code I'm afraid... do you have a small example project which shows this behaviour?  If you can send me something like that, I'd be glad to give it a look!

Jun 16, 2009 at 3:04 PM

As for the first question: fantastic. I appreciate the quick response and even faster code release.

As for the second question: let me get something together and I'll pass it your way. :)