Articles in this section
Category / Section

How to Restrict Node is being removed from its Parent Container when Dragging in WPF Diagram (SfDiagram)

3 mins read

WPF Diagram (SfDiagram) supports restricting the Node is being removed from its parent container. This can be achieved by using NodeChangedEvent. We have provided a sample to achieve this requirement.

In the code sample below, we have used the CustomNodeVM class with the ParentContainer property, representing the node’s parent container. The value for ParentContainer will be set using ItemAddedEvent. In the NodeChangedEvent, we have used BoundaryConstraints property to achieve this requirement.

code Snippet:

//Adding Item Added Event. Here diagram is an instance of the SfDiagram.
(diagram.Info as IGraphInfo).ItemAdded += MainWindow_ItemAdded;

//Adding Node Changed Event.
(diagram.Info as IGraphInfo).NodeChangedEvent += MainWindow_NodeChangedEvent;

// Method for executing the item added event.
private void MainWindow_ItemAdded(object sender, ItemAddedEventArgs args)
{
   if (args.Item is ContainerViewModel)
   {
       var container = (ContainerViewModel)args.Item;
       if ((container.Nodes as IEnumerable<object>).Any())
       {
           foreach (var item in container.Nodes as IEnumerable<object>)
           {
               var node = item as CustomNodeVM;
               //setting parent container to the nodes.
               node.ParentContainer = container;
           }
       }
   }
}

//Method for executing the node changed event.
private void MainWindow_NodeChangedEvent(object sender, ChangeEventArgs<object, NodeChangedEventArgs> args)
{
   //Setting boundary constraint to node when it is dragging to restrict nodes dragging outside of the container.
   if (args.Item is CustomNodeVM && !(args.Item is ContainerViewModel))
   {
       var node = args.Item as CustomNodeVM;
       if (node.ParentContainer != null && args.NewValue.InteractionState == NodeChangedInteractionState.Dragging)
       {
           ContainerViewModel parentcontainer = node.ParentContainer as ContainerViewModel;
           Rect parentContainerBounds = (parentcontainer.Info as IContainerInfo).Bounds;
           //Setting Node’s parent container bounds region value to BoundaryConstraint property.
           args.BoundaryConstraints = new Rect(parentContainerBounds.Left + 5, parentContainerBounds.Top + 45,
           parentContainerBounds.Width - 10, parentContainerBounds.Height - 50);
       }
   }
}

RestrictNodeFromContainer-ezgifcom-video-to-gif-converter.gif

View Sample in GitHub

Conclusion:
I hope you enjoyed learning how to restrict a node from being removed from its parent container when dragging it into the WPF Diagram (SfDiagram).
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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied