Articles in this section
Category / Section

How to Prevent Node from Moving Outside the Group in WPF Diagram?

4 mins read

In the WPF Diagram, you can prevent nodes from moving outside their designated groups by overriding the PointerSelection method in a custom Selector class. Within this method, use the ClearSelection method to reset the selection if a node is moved outside its group. This prevents the node from being repositioned outside its designated area. Below is the provided code snippet to achieve this functionality.

Code Snippet:

//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);
           }
       }
   }
} 

ScreenCapture_1-15-202554705PM-ezgifcom-crop.gif

Conclusion
I hope you enjoyed learning about how to prevent the node from moving outside 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, You can also explore our WPF Diagram example to understand how to create and manipulate data.

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