2 RadtreeViews as explorer and drag and drop items (files or folders) from one to another

1 Answer 15 Views
TreeView
Danny
Top achievements
Rank 1
Iron
Danny asked on 14 Apr 2025, 06:46 AM

Hi,

I tried to combine the 2 telerik examples related to RadTreeviews - the RadTreeView as File Explorer and that from SDK repo related to drag and drop between 2 Radtreeview.

I finally succeeded to make an working short program, but I don't know why by dragging a file from one tree to another, the UI of the second tree shows the dropped file twice - even the Tree ItemsSource is correct (only one file dragged and dropped)

after Puplating the trees,I used DragDropMAnager and the following classes (as they are defined in telerik example, adapted to files/folder structure)

DragDropManager.AddDragOverHandler(this.tree1, OnTree1DragOver, true);
DragDropManager.AddDragOverHandler(this.tree2, OnTree2DragOver, true);
DragDropManager.AddDropHandler(this.tree2, OnTree2Drop, true);

My OnTree2DragOver function:

var options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
if (options == null)
    return;

// The condition after the first OR operator is needed to deny the drop of items in Application File. (sub-items)
RadTreeViewItem dropTargetItem = options.DropTargetItem;

var draggedItem = options.DraggedItems.First();
if (dropTargetItem == null ||
(dropTargetItem != null &&
     options.DropTargetItem.DataContext is File &&
     options.DropPosition == DropPosition.Inside) ||
    draggedItem is Folder)
{
    options.DropAction = DropAction.None;
}

options.UpdateDragVisual();

 

 

my OnTree2Drop function is:

 var options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
 if (options == null)
     return;
 File draggedItem = options.DraggedItems.FirstOrDefault() as File;
 if (draggedItem == null)
     return;
 RadTreeViewItem dropTargetItem = options.DropTargetItem;
 if (dropTargetItem == null)
     return;
 var dropItemModel = dropTargetItem.DataContext;
 if (dropItemModel == null)
     return;
 var dropTree = sender as RadTreeView;
 if (dropTree != null)
 {
     // Disable drop in tree2 File.
     if (dropItemModel is File && options.DropAction == DropAction.None)
     {
         e.Handled = true;
         return;
     }
     //Drop in tree2
     if (dropItemModel is Folder || dropItemModel is File)
     {
         options.DropAction = DropAction.Copy;
         //options.UpdateDragVisual();
         Folder destinationFolder = null;
         if (dropItemModel is Folder)
         {
             // Dropping inside Application.
             destinationFolder = dropItemModel as Folder;
         }
         else
         {
             // Dropping Before or After an Application Resource.
             destinationFolder = options.DropTargetItem.ParentItem.DataContext as Folder;
         }
         if (destinationFolder == null)
         {
             return;
         }
         File file = new File()
         {
             Name = draggedItem.Name
         };
         destinationFolder.Items.Add(file);
    
         //options.UpdateDragVisual();
     
         System.IO.File.Copy(@draggedItem.FullPath, System.IO.Path.Combine(@destinationFolder.FullPath, System.IO.Path.GetFileName(@draggedItem.FullPath)));
         options.UpdateDragVisual();

My OnTree1DragOver:

var options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
if (options != null)
{
    options.DropAction = DropAction.None;
    options.UpdateDragVisual();
    var draggedFile = options.DraggedItems.First() as File;
    e.Handled = true;
}

In the attached picture you'll see Adeliatte Italic.ttf twice, even I dragged and drpped it only once.

Could you please take a short look and tell me what I'm doing wrong?

Or, if it is not too much time consuming for you to come back with an example of 2 treeviews acting as explorer and with d&d functionality?

 

Thank you in advance for your support.

Danny

1 Answer, 1 is accepted

Sort by
0
Danny
Top achievements
Rank 1
Iron
answered on 14 Apr 2025, 07:24 PM

Finally, I saw were was the mistake: I used an update of Tree2 ItemsSource (the target tree) which couldn't be there .

Danny

Tags
TreeView
Asked by
Danny
Top achievements
Rank 1
Iron
Answers by
Danny
Top achievements
Rank 1
Iron
Share this question
or