Articles in this section
Category / Section

How to Prevent Nodes from being Unselected when Right-Clicking in WPF SfDiagram?

3 mins read

In the WPF Diagram, you can prevent nodes from being unselected when right-clicking on the diagram by using the ItemUnSelecting and PreviewMouseRightButtonUp events. In the ItemUnSelecting event, you can manage the unselection behavior to maintain the selection state of the node by setting the true for the args. Cancel property. We have provided the code example for how to achieve this.

private bool rightMouseButtonUp = false;
private int selectedNodesCount = 0;

//Here diagram is the instance of the SfDiagram

diagram.PreviewMouseRightButtonUp += Diagram_PreviewMouseRightButtonUp;
(diagram.Info as IGraphInfo).ItemUnSelectingEvent += MainWindow_ItemUnSelectingEvent;

       private void MainWindow_ItemUnSelectingEvent(object sender, DiagramPreviewEventArgs args)
       {
           if (rightMouseButtonUp)
           {
               args.Cancel = true;
           }
           if (rightMouseButtonUp)
           {
               selectedNodesCount--;
           }
           if (selectedNodesCount == 0 && rightMouseButtonUp)
           {
               rightMouseButtonUp = false;
           }
       }


       private void Diagram_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
       {
           NodeViewModel node = null;
           ConnectorViewModel con = null;
           if(e.OriginalSource is Path)
           {
               if((e.OriginalSource as Path).DataContext is NodeViewModel)
               {
                   node = (e.OriginalSource as Path).DataContext as NodeViewModel;
               }
               if((e.OriginalSource as Path).DataContext is ConnectorViewModel)
               {
                   con = (e.OriginalSource as Path).DataContext as ConnectorViewModel;
               }
           }

           if (e.Source is SfDiagram && (node == null && con == null))
           {
               rightMouseButtonUp = true;
               selectedNodesCount = (((e.Source as SfDiagram).SelectedItems as SelectorViewModel).Nodes as IEnumerable<object>).Count();
           }
       } 

ScreenCapture_12-9-202461439PM-ezgifcom-video-to-gif-converter.gif

Conclusion
I hope you enjoyed learning about how to prevent nodes from being unselected when right-clicking in WPF 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