WPF: how to implement drag and drop with nested (hierarchical) controls

Using WPF, I was able to implement drag and drop to reorder items in a list (view or window), as well as drag items between lists.

Now I'm trying to figure out how to implement drag and drop using NESTED lists.

For example, I have a list containing projects, and each element of the project contains a different list of tasks. I want to be able to drag and drop to reorder projects, as well as reorder tasks and move them between projects.

I have code that successfully executes one of the others, but I cannot figure out how to do this.

There seems to be some kind of painful solution that would include testing for success and possibly the z-order of nested lists, but I can't find examples of this.

Can anyone suggest any pointers?

FYI: The working code that I have just implemented is based on the following two excellent WPF drag and drop articles:

http://bea.stollnitz.com/blog/?p=53 http://www.codeproject.com/KB/WPF/ListViewDragDropManager.aspx

+7
wpf silverlight
source share
4 answers

Since MouseMove and most of the others in wpf are routable events, you can simply check e.OriginalSource in a common event handler. Then you can decide which element to drag and which element the mouse is based on, possibly using one of those search methods "find parent that satisfies the condition". In addition, you can install e.Handled if you have several elements in the visual tree that subscribe to the event.

+4
source share

Just first thoughts: why not use TreeView instead of ListView if you have nesting?

+2
source share

AllowDrop must be true for any control.

0
source share

I encountered a similar problem when working with an application with lists of nested user controls.
I handled all this in the PreviewMouseButtonDown event on one control level. I check the coordinates of the point that was clicked. If it came from somewhere in the parent ListBoxItem that was not in the ListBox , I process DragDrop.DoDragDrop() there. If it appears inside the ListBoxItem , I allow it to be dumped into the child ListBox PreviewMouseButtonDown event . I check where the location is in the child ListBox to see which item was clicked so that I can capture it and instead make DragDrop at that level.

The pseudocode is as follows:

 Parent ListBox -- PListBoxItem1 -- PListBoxItem2 -- PListBoxItem3 ---- Child ListBox ------ Child ListBoxItem1 ------ Child ListBoxItem2 -Click drag started here ------ Child ListBoxItem3 

the code:

 Parent_List_Box_PreviewMouseButtonDown If mouse position is not inside the Child ListBox Then DoDragDrop() on the Parent level with this ListBoxItem End If Child_ListBox_PreviewMouseButtonDown Determine which item the mouse was clicked on relative to the Child ListBox DoDragDrop() on the Child level with this ListBoxItem 

Since click was inside the Child ListBox , the event bubbles to the lowest handler that passes the criteria for the DragEvent .

Hope this helps!

0
source share

All Articles