Drag and Drop in MVVM
Hi
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;
return;
}
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
}
SIGN IN To post a reply.
3 Replies
SS
Susmitha Sundar
Syncfusion Team
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.
C#:
|
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;
viewModel.Employees.Remove(memNode);
memNode.ReportsTo = targetLeaf.ID;
viewModel.Employees.Add(memNode);
e.Handled = true;
} |
Sample link: https://www.syncfusion.com/downloads/support/directtrac/147844/ze/SfTreeGrid_DragDrop_DupicateParent888958575
Please get back to us if you require further other assistance from us.
Regards,
Susmitha S
MI
Michele
September 27, 2019 07:24 AM UTC
Thanks
Everything works fine.
Very professional and fast support.
Best regards
Michele
FP
Farjana Parveen Ayubb
Syncfusion Team
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.
Regards,
Farjana Parveen A
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
-
MI Michele
- Sep 25, 2019 02:46 PM UTC
- Sep 27, 2019 07:29 AM UTC