I still learning a lot about C# and Telerik. I am trying to figure out what NODE I am moving in a TREEVIEW when I am done moving it. Any node in my tree can move and any node can be in any position (ROOT or CHILD). I have looked at DragEnded event, but the sender is never the node I have selected and/or moved.I have looked at SelectedNodeChanged, but that fires multiple times during a drag event and while the last one is the correct node, I don't believe this is the correct direction. As for DragDrop even, this never fires, which I found both weird and interesting, as I have both AllowDragDrop and AllowDrop turned on for the tree view. I am currently using Telerik Winforms version 2016.2.503.40.
Thanks in advanced.
Mark
12 Answers, 1 is accepted
Thank you for writing.
The TreeView has it own DragDropService which is responsible for the drag and operation. In your case, you can use the PreviewDragDrop event:
public
RadForm1()
{
InitializeComponent();
radTreeView1.AllowDragDrop =
true
;
radTreeView1.TreeViewElement.DragDropService.PreviewDragDrop += DragDropService_PreviewDragDrop;
}
private
void
DragDropService_PreviewDragDrop(
object
sender, Telerik.WinControls.RadDropEventArgs e)
{
var draggedNode = e.DragInstance
as
TreeNodeElement;
Console.WriteLine(draggedNode.Data.Text);
}
I hope this will be useful. Let me know if you have additional questions.
Regards,
Dimitar
Telerik by Progress
.jpg)
.jpg)
Thanks for the help, I figured it out from the sample code in that link I posted..
I am glad that you have found a solution for your case. Do not hesitate to contact us if you have other questions.
Dimitar
Telerik by Progress
.jpg)
Dimitar:
I have almost everything working, made a lot of changes to use a List<> vs DT, which made it a lot cleaner. However, I have a question, We are using the RadBreadCrumb as well. The problem is when we drag/drop and we get into the OnPreviewDragDrop of the CustomDragDropService, it crashes when we set the DataSource of the treeview to NULL, and that's because the RadBreadCrumb.DefaultTreeView is referencing the treeview, which now has no data. Is there a way around this? We really would like to use the RadBreadCrumb object. I have tried the "DragStarted" and "DragEnded" events, but the DragEnded event never fires.
Thanks
You should set the DefaultTreeView to null before changing the data source:
radBreadCrumb1.DefaultTreeView =
null
;
radTreeView1.DataSource =
null
;
radTreeView1.DataSource = dataItems;
radBreadCrumb1.DefaultTreeView = radTreeView1;
Let me know if I can assist you further.
Regards,
Dimitar
Telerik by Progress
.jpg)
You can create a property in your CustomDragDropService class:
class
CustomDragDropService : TreeViewDragDropService
{
private
RadTreeViewElement owner;
private
List<RadTreeNode> draggedNodes;
private
RadBreadCrumb crumb;
public
RadBreadCrumb Crumb
{
get
{
return
this
.crumb;
}
set
{
this
.crumb = value;
}
}
public
CustomDragDropService(RadTreeViewElement owner) :
base
(owner)
{
this
.owner = owner;
this
.crumb = crumb;
}
}
Then you can set it like this:
((CustomDragDropService)radTreeView1.TreeViewElement.DragDropService).Crumb = radBreadCrumb1;
Let me know if I can assist you further.
Regards,
Dimitar
Telerik by Progress
.jpg)
Thanks, this was helpful. I did run into one other issue in the DragDropService, and that was know the tree view that service belonged too. I could not use "owner" as that was a treeviewelement object, so I did what you did with the BreadCrumb object. Was that the correct direction?
Thanks
.jpg)
Here is an easier way to get the parent TreeView:
protected
override
void
OnPreviewDragDrop(RadDropEventArgs e)
{
base
.OnPreviewDragDrop(e);
this
.crumb.DefaultTreeView =
null
;
this
.crumb.DefaultTreeView = owner.ElementTree.Control
as
RadTreeView;
}
Please let me know if there is something else I can help you with.
Regards,
Dimitar
Telerik by Progress
.jpg)