Articles in this section
Category / Section

How to switch between tools at runtime through the SetTool in WPF Diagram (SfDiagram)?

2 mins read

Each object in the WPF Diagram (SfDiagram) control has default action when interacting on them. Those default actions can be customized by overriding the virtual method SfDiagram.SetTool. The SetTool method takes the SetToolArgs as an argument that is used to know the objects under the mouse when modifying the tools of them.

Source –  To know the object on which item the mouse is interacting.

Action - To customize the tools of the diagram object.

C#

public class CustomDiagram : SfDiagram
{                
  protected override void SetTool(SetToolArgs args)
  {
    if (args.Source is INode || args.Source is IConnector || args.Source is DiagramPage)
    {
      //the boolean property in ViewModel will get update based on the radio button command binding.
      if ((DataContext as DiagramVM)._singleselect)
      {
        args.Action = ActiveTool.Drag;
      }
      else if ((DataContext as DiagramVM)._multipleselect)
      {
        args.Action = ActiveTool.RubberBandSelection;
      }
      else if ((DataContext as DiagramVM)._none)
      {
        args.Action = ActiveTool.None;
      }
      else if ((DataContext as DiagramVM)._zoompan)
      {
        args.Action = ActiveTool.Pan;
      }
      else if ((DataContext as DiagramVM)._draw)
      {
        args.Action = ActiveTool.Draw;
      }
    }
    else
    {
      base.SetTool(args);
    }
  }
}

 

ToolSelection

View sample in GitHub.

 

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