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

Disallow dragging to a specific node

6 Answers 95 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Serg
Top achievements
Rank 1
Veteran
Serg asked on 19 Jan 2021, 01:08 PM
How can I prevent dragging to a specific node? (by type of folders and files). Folders are nodes where you can put another node, and files are nodes where you can't put another node.

6 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Jan 2021, 01:32 PM
Hello, Serg, 

RadTreeView handles the whole drag and drop operation by its TreeViewDragDropService. The PreviewDragOver event allows you to control on what targets the node element being dragged can be dropped on. If the target node is not allowed, set the CanDrop argument to false.

Please refer to the following help articles which will get you familiar with the TreeViewDragDropService and how you can control the drag and drop operation if some custom requirements occur:
https://docs.telerik.com/devtools/winforms/controls/treeview/drag-and-drop/treeviewdragdropservice 
https://docs.telerik.com/devtools/winforms/controls/treeview/drag-and-drop/drag-and-drop-in-bound-mode 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Serg
Top achievements
Rank 1
Veteran
answered on 19 Jan 2021, 01:45 PM

I didn't quite understand how to use it. I have already read this documentation.And only found the DragEnding event there. But TargetNode does not always indicate the parent Node where the dragged node is moved, and sometimes just the adjacent one when changing the order. 
I made a special class with isDir property (He says whether it is possible to move the nodes inside). But I don't understand how to use it in DragEnding :(

public class RadTreeNode2 : RadTreeNode
    {
        public bool isDir = false;
 
        public RadTreeNode2()
        {
        }
        public RadTreeNode2(string text) : base(text)
        {
             
        }
    }

0
Serg
Top achievements
Rank 1
Veteran
answered on 19 Jan 2021, 03:30 PM
protected override void OnPreviewDragOver(RadDragOverEventArgs e)
        {
            base.OnPreviewDragOver(e);
            e.CanDrop = (e.HitTarget as RadTreeNode2).isDir;
        }

 

This code not work :(

0
Serg
Top achievements
Rank 1
Veteran
answered on 19 Jan 2021, 03:51 PM

I also found this kind of documentation, but for some reason it does not work.Why it happens?

Using AllowDrop property
You can interrupt a drag and drop operation by setting the AllowDrop property of a specific RadTreeNode to false. This way you still can drag this particular node, but you cannot add other nodes to it with drag and drop operation. You can set this for any particular node you want.
0
Serg
Top achievements
Rank 1
Veteran
answered on 19 Jan 2021, 04:18 PM
Sorry, it turns out that the "AllowDrop property" really works. And it does what I wanted to implement in the first post in this thread :)
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Jan 2021, 12:16 PM
Hello, Serg, 

Overriding the OnPreviewDragOver method of the custom TreeViewDragDropService or just subscribing to the TreeViewDragDropService.PreviewDragOver event is the appropriate solution to disable dragging over certain nodes. In the event arguments you have access to the target visual node element from where you can access the data node and determine whether the drop operation is allowed by its "isDir" value.

Specifying the AllowDrop property for the desired RadTreeNodes is also a suitable approach: https://docs.telerik.com/devtools/winforms/controls/treeview/drag-and-drop/cancel-a-drag-and-drop-operation 

Feel free to use this approach which suits your requirements best. In case you are experiencing any further difficulties with the drag and drop functionality, it would be greatly appreciated if you can provide a sample project demonstrating the problem you are facing. Thus, we would be able to investigate the precise case and provide further assistance.    

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Treeview
Asked by
Serg
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Serg
Top achievements
Rank 1
Veteran
Share this question
or