Drag and Drop Treeview Items between 2 Treeviews
Hello
I am trying to drag treeview items from 1 treeview to but no luck. I am unable to find the event ItemDrag like in the control found in VS.Net 2003.
Hello
I am trying to drag treeview items from 1 treeview to but no luck. I am unable to find the event ItemDrag like in the control found in VS.Net 2003.
I believe you're looking for the DragLeave event which is inherited from the base class UIElement.
HTH,
Drew
Maybe just a brief explanation on how I go about populating the first treeview. What I do is in the code I add TreeViewItems that can be a collection of multiple TreeViewItems. When I am Starting to do the Dragging of a selected TreeViewItem, I make use of the following piece of code:
DragDrop.DoDragDrop(this, TreeView1.SelectedItem, DragDropEffects.Copy | DragDropEffects.Move);
With a DragOver Event on the second treeview I ensure that what is being dragged is a TreeViewItem, but when on the second treeview the mouse Cursor shows that I am unable to drop the dragged Item onto the TreeView control. I also made sure that the AllowDrop = true on the second Treeview.
I have tried the DragLeave on the second treeview but it doesn't seem to fire correctly.
Gerhard
Just an question regarding the DragLeave Event. Am I correct in saying that the DragLeave event is raised when the user drags the cursor out of the control or the user cancels the current drag-and-drop operation and that the DragAction is Cancel when the DragLeave event is raised.
Then just as a add on to my previous post, Am I correct int using a TreeViewItem as Data Object assigned to the doDragDrop event?
Gerhard
I added a TextBox on the form with the 2 Treeviews and when I drag a treeview item from the first treeview, the Drop event fires for the textbox and the Data object that has been passed is actually the TreeViewItem that is selected in the first Treeview. The Cursor when dragging the TreeViewItem over the Textbox changes to indicate that I can drop the dragged item while on the second treeview the cursor changes to indicate that I am unable to drop the dragged Item.
This brings me to the following question: Is the Drop event been fully implemented on the TreeView control?
Any ideas on how I can get this to work?
Gerhard
On the second treeview there is to events I have:
1. DragOver
2. Drop
When I add a break point in the DragOver event and actaully drag an item from the first to the second treeview, and step the code once the break point in the DragOver event is hit, the Drop Event is fired, but with out the break point, the event is not fired and the cursor indicates that I can not drop.
AllowDrop is set to true on the Second Treeview.
Is there any explanation for this?
Any help would be appreciated.
Gerhard
Gerhard, let me clarify something about (Rich)TextBox, TreeView and TreeViewItem. RichTextBox and TextBox natively implement drag/drop handlers, so you can move text around out-of-the-box. There are pretty clear semantics about what it means to move text from one such control to another.
TreeView and TreeViewItem are different beasts. Typically they represent something else other than a piece of UI, and especially in Avalon, they are very likely to be data-bound to something else. You would normally want to update the backing data and let your data-bound UI update accordingly, although this is not always the case. The short of it is that TreeViewItem does not have an OOB implementation for drag/drop, and that's what you're seeing.
Implementing drag/drop can be as simple as firing DragDrop, which you're already doing, setting AllowDrop to True, and adding handlers to update the cursor and perform the selected operation. You'll need to extract the item from its parent and reattach it to the new parent in your handler.
I'll be posting a sample on my blog showing how you can drag / drop TreeView items within the next day or so.
Marcelo
Thank you for your reply.
If you could give me the URL to your blog, it will be appreciated.
Gerhard
I upgraded from the Jan 2006 CTP to the Feb 2006 CTP and now the drag and drop event is executing correctly. I can now drag a TreeViewItem from the one TreeView to another TreeView in the Feb 2006 where I was unable to do this in the Jan 2006 CTP.