How to manage the node's visibility when it is dropped onto another node in WPF Diagram (SfDiagram)?
In the WPF Diagram, you can manage a node’s visibility when it is dropped onto another node by collapsing the visibility of the dropped node when it is dropped on the another node, using the ItemDrop event. The relative positions of the dropped nodes will be updated in the same way as the parent node, utilizing the NodeChanged event. We have provided the code example for how to achieve this.
Code Snippet:
// Here the diagram is the instance of the SfDiagram
(diagram.Info as IGraphInfo).ItemDropEvent += MainWindow_ItemDropEvent;
(diagram.Info as IGraphInfo).NodeChangedEvent += MainWindow_NodeChangedEvent;
//Method to add a dropped node as a child as an intersected node.
private void MainWindow_ItemDropEvent(object sender, ItemDropEventArgs args)
{
if (args.Source is CustomNodeViewModel && !(args.Target is SfDiagram))
{
var childNode = args.Source as CustomNodeViewModel;
foreach (var node in args.Target as IEnumerable<object>)
{
if (node is CustomNodeViewModel)
{
var parentNode = node as CustomNodeViewModel;
//Add dropped node as child of intersected node
parentNode.ChildNodes.Add(childNode as CustomNodeViewModel);
//Enable selection for the parent and disable selection for the child which is collapsed.
childNode.NodeVisibility = Visibility.Collapsed;
childNode.IsSelected = false;
parentNode.IsSelected = true;
}
}
}
}
//Method to update the relative position of the parent node to the child node.
private void MainWindow_NodeChangedEvent(object sender, ChangeEventArgs<object, NodeChangedEventArgs> args)
{
if (args.NewValue.InteractionState == NodeChangedInteractionState.Dragged)
{
var parentnode = args.Item as CustomNodeViewModel;
if (parentnode.ChildNodes != null && parentnode.ChildNodes.Count > 0)
{
foreach(CustomNodeViewModel childNode in parentnode.ChildNodes as IEnumerable<object>)
{
childNode.OffsetX = parentnode.OffsetX;
childNode.OffsetY = parentnode.OffsetY;
}
}
}
}
Conclusion
I hope you enjoyed learning about how to manage the node’s visibility when it is dropped onto another node in the WPF Diagram
You can refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!