Articles in this section
Category / Section

How to Restrict the Node Being Dragged from the Group in WPF Diagram?

4 mins read

In WPF Diagram, you can restrict Node from being dragged outside their parent Group by creating a custom selector class that inherits from the Selector class. By overriding the PointerSelection method, you can implement logic that checks if the selected element is a Node with a ParentGroup. The selection behavior then depends on whether the parent Group is already selected. If the parent Group is selected, the selection of the Node is cleared using the ClearSelection method, preventing it from being dragged outside the Group. Below is the code snippet for implementing this functionality.

C# Code Snippet:

// Custom class for SfDiagram
public class CustomDiagram : SfDiagram
{
   //Method to return the selector for diagram
   protected override Selector GetSelectorForItemOverride(object item)
   {
       //Assigning custom selector to the diagram
       CustomSelector selector = new CustomSelector();
       return selector;
   }
}

// Custom class for selector
public class CustomSelector : Selector
{
   //Method to decide whether the Selection should be done in PointerDown.
   protected override void PointerSelection(PointerSelectionArgs args)
   {
       if (args.PointerMode == PointerMode.Down)
       {
           //Check whether if we select the Node or not
           if (args.Source is NodeViewModel)
           {
               NodeViewModel node = args.Source as NodeViewModel;

               //Check whether the parent group of the parent is null or not
               if (node.ParentGroup != null)
               {
                   //Ensure the IsSelected Property of the Parent Group of the Node.
                   if ((node.ParentGroup as GroupViewModel).IsSelected)
                   {
                       //Clear the Selection if we select the Node
                       ClearSelection(Element: args.Source);
                   }
                   else
                   {
                       Selection(element: args.Source);
                   }
               }
               else
               {
                   Selection(element: args.Source);
               }
           }
           else if (args.Source is ConnectorViewModel)
           {
               Selection(element: args.Source);
           }
           else if(args.Source is SfDiagram)
           {
               //Clear the selelction if we select the diagram
               ClearSelection(Element: args.Source);
           }
       }
   }
} 

Group with UnDraggable Child nodes

View Sample in GitHub

Conclusion
I hope you enjoyed learning about how to restrict the node being dragged from the group in 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!

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