We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Drag and Drop in MVVM

Thread ID:





147844 Sep 25,2019 02:46 PM UTC Sep 27,2019 07:29 AM UTC WPF 3
Tags: SfTreeGrid
Asked On September 25, 2019 02:46 PM UTC

My SfTreeGrid is binded to an ObservableCollection<MyObject>
MyObject contains an ObservableCollection<MyObject> Leafs and so on.
When i add new objects to Leafs programmatically, the TreeGrid updates correctly by notification from ViewModel.

When i perform a Drag and Drop, the node dropped is "moved" automatically to its new parent and ObservableCollection Leafs is not updated.
I assume that this is a usual sfTreeGrid behavior.

But if i programmatically  update the ObservableCollection Leafs, after Drag and Drop operation, the binding notification create a duplicate node in the parent Node. I think because the new parent Node doesn't notify the new child node to the ObservableCollection.

So, how is it possibile to avoid or manage this behavior?
For example, i don't need the automatic addition of the node by sfTreeGrid. 

This is my code
        private MyObject TempDrag{get;set;}
        private void NodeDragStart(object sender, TreeGridRowDragStartEventArgs e)
            TempDrag= e.DraggingNodes[0].Item as MyObject;

        private void NodeDragOver(object sender, TreeGridRowDragOverEventArgs e)
            var dropPosition = e.DropPosition.ToString();
            if (dropPosition == "None") return;
            if (dropPosition == "DropAbove") return;
            if (dropPosition == "DropBelow") return;

            var targetLeaf = e.TargetNode.Item as MyObject;

        private void NodeDrop(object sender, TreeGridRowDropEventArgs e)
            var dropPosition = e.DropPosition.ToString();
            if (dropPosition == "None") return;
            if (dropPosition == "DropAbove") return;
            if (dropPosition == "DropBelow") return;
            if (e.IsFromOutSideSource) return;

            var targetLeaf = e.TargetNode.Item as MyObject;

            if (targetLeaf.Type.ToString() != "Document" && targetLeaf.Type.ToString() != "Array")
                e.Handled = true;

            var memNode = e.DraggingNodes[0].Item as MyObject;
            var parentMemNode = memNode.Parent;
            targetLeaf.AddLeaf(memNode);  //Here i add MyObject to the new Parent but it cause a duplicate node in the sfTreeView
            parentMemNode.DelLeaf(memNode); //Here i remove MyObject from the old Parent 

Susmitha Sundar [Syncfusion]
Replied On September 26, 2019 11:23 AM UTC

Hi Michele,   
Thank you for using Syncfusion control.   
We can able to reproduce the reported issue. By default, if you drag and drop the records it will be moved automatically. If you want to move the records manually, you should handle the Drop event in your application level. So, After adding the records to the collection, you need to set e.Handled as true in Drop event.   
Please refer the below code snippet.  
private void RowDragDropController_Drop(object sender, Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDropEventArgs e)   
            var dropPosition = e.DropPosition.ToString();   
            if (dropPosition == "None") return;   
            if (dropPosition == "DropAbove") return;   
            if (dropPosition == "DropBelow") return;   
            if (e.IsFromOutSideSource) return;   
            var targetLeaf = e.TargetNode.Item as EmployeeInfo;   
            var memNode = e.DraggingNodes[0].Item as EmployeeInfo;   
            var viewModel = this.DataContext as ViewModel;   
            memNode.ReportsTo = targetLeaf.ID;   
            e.Handled = true;   
Please get back to us if you require further other assistance from us.   
Susmitha S   

Replied On September 27, 2019 07:24 AM UTC

Everything works fine.

Very professional and fast support.

Best regards

Farjana Parveen Ayubb [Syncfusion]
Replied On September 27, 2019 07:29 AM UTC

Hi Michele 
Thanks for the update. 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
Farjana Parveen A 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon