This is a migrated thread and some comments may be shown as answers.

Drag/Drop - not deleting the dragged object

9 Answers 146 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
GEB
Top achievements
Rank 1
GEB asked on 16 Jan 2009, 06:26 PM
When dragging and dropping an object, I want to control whether the original object that was dragged is deleted from its original position in the RadTreeView.

For example, if I drag and object within a note, I want the original position to be deleted, and replaced by the object's new position within the node.

But, if the object is dropped into a different node in the RadTreeView, I may want the object to not be deleted, and remain in the original node, as well as being dropped in the new node.  Any way to accomplish this?

Also, any way to control the image size in RadTreeViewItem?  For example, I want to associate a PNG image with a RedTreeViewItem, and I want it to be 48x48.  Can this be done?

Any finally, if I associate a color with the background of a RadTreeViewItem, it changes the background all the way to the left of the RadTreeView.  Any way to have the background color only start at the indent position of the RadTreeViewItem?

9 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 26 Jan 2009, 11:32 AM
Hi Gary,

You can manipulate the drag/drop functionality within a treeview in the way you describe by listening to the PreviewDragEnded and DragEnded events. Refer to the attached project to see how this is achieved.

As for your questions regarding icons and background in TreeViewItems - this is achievable by changing the control template of the items. I've demonstrated this in the sample project as well.

Let me know if this solves your issues.

Sincerely yours,
Tihomir Petkov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
James
Top achievements
Rank 1
answered on 26 Mar 2009, 08:16 PM
Hi, I'm just working through this example and am finding in my application that in treeView_PreviewDragEnded, e.DraggedItems is not a RadTreeViewItem, but the business object that the node is bound to. Is this expected, or has something gone wrong?

Thanks, James.
0
Tihomir Petkov
Telerik team
answered on 30 Mar 2009, 08:18 AM
Hi James,

What you describe is the expected behavior - the e.DraggedItems collection, and the treeview.Items one for that matter, contains the business objects (unless you populate the treeview with a collection of treeview items, of course). If you need to get the treeview item that wraps your business object you should call the ItemContainerGenerator.ContainerFromItem() method of the immediate parent of your item e.g. treeViewItem.ItemContainerGenerator.ContainerFromItem(childItem) or treeView.ItemContainerGenerator.ContainerFromItem(rootItem) if your item is at the root level.

Let me know if this solves your problem.

Sincerely yours,
Tihomir Petkov
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Sukhvinder Pal
Top achievements
Rank 1
answered on 16 Sep 2009, 07:39 AM
I have the same problem. And when I try to get the parent item of the dragged item by the following
RadTreeViewItem target = e.TargetDropItem;
            Node draggedNode = e.DraggedItems[0] as Node;
            RadTreeView sourceTree = e.OriginalSource as RadTreeView;
            object treeItem = sourceTree.ItemContainerGenerator.ContainerFromItem(draggedNode);

I get the treeItem as null. How do I get the parent item of the dragged Item?
0
Miroslav
Telerik team
answered on 16 Sep 2009, 07:49 AM
Hello Sukhvinder Pal,

This call:

sourceTree.ItemContainerGenerator.ContainerFromItem(...)

will return the item container only if it is an immediate child of the TreeView. You can recursively search the containers by using the TreeViewItem.ContainerFromItemRecursive(item) method.

Hopefully this will help you,

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Sukhvinder Pal
Top achievements
Rank 1
answered on 16 Sep 2009, 08:06 AM
RadTreeViewItem has no member ContainerFromItemRecursive. 
0
Miroslav
Telerik team
answered on 16 Sep 2009, 08:52 AM
Hi Sukhvinder,

I am sorry for my mistake, this is a method of the TreeView and not the TreeViewItem.

Greetings,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Sukhvinder Pal
Top achievements
Rank 1
answered on 16 Sep 2009, 09:45 AM
Another problem:

I have two trees. And I am drgging from one tree to another. The following does not give me the source tree. Rather it gives me the tree where the drop is taking place.
RadTreeView sourceTree = e.OriginalSource as RadTreeView;

How do I get the source tree?

Even after hacking my way to get the source tree, The following does not give me the dragged item (returns null)
RadTreeViewItem draggedItem = sourceTree.ContainerFromItemRecursive(draggedNode);

Is there any other solution to prevent the source item to be deleted. This is really urgent...
0
Miroslav
Telerik team
answered on 16 Sep 2009, 11:50 AM
Hello Sukhvinder Pal,

Yes, this will give you the source of the drop.

When you drag between TreeViews the  PreviewDragEnded event will be fired twice - once for each TreeView.

If you handle the preview event on the source tree, the dragged item will not be removed and an apparent copy will occur.

If you handle the preview event on the destination tree, the item will not be added and an apparent deletion will occur.

So in the PreviewDragEnded event you can check whether the sender is the same as the TreeView of the drop destination, like so:

void treeView_PreviewDragEnded(object sender, RadTreeViewDragEndedEventArgs e)  
{  
    if (e.TargetDropItem.ParentTreeView != sender)  
    {  
        e.Handled = true;  
    }  

Does this work for you?

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
GEB
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
James
Top achievements
Rank 1
Sukhvinder Pal
Top achievements
Rank 1
Miroslav
Telerik team
Share this question
or