How to create command with Sub menu in custom 'NDiagramContextMenuBuilder;


https://www.nevron.com/Forum/Topic12098.aspx
Print Topic | Close Window

By Niranjan Singh - 7 Years Ago
I have further requirement from my last implementation using this - How to remove auto generated context menu items for the selected shape conditionally?

Now I need to add some extra menu items to the context menu. I have added these command with sub menu but when i click these menu item then it does not fire the CommandContextExecuted event as like Delete and other commands. Below is the code snippet to create extra menu items in the context menu..

internal class CustomContextMenuBuilder : NDiagramContextMenuBuilder
  {
   public override NContextMenu BuildContextMenu(object obj)
   {
    NContextMenu contextMenu = null;
    if (obj as NDrawingDocument == null)
    {
      contextMenu = new NContextMenu();
      /* Added Delete option in the context menu */
      contextMenu.Commands.Add(CreateCommand((int)DiagramCommand.Delete, false));

      bool isConnectorMenu = obj as NRoutableConnector != null;
      if (isConnectorMenu)
       contextMenu.Commands.Add(CreateCommand((int)DiagramCommand.Reroute, true)); /* Added Reroute option in the context menu if connectors*/

      /* Added SubmenuOrder in the context menu */
      contextMenu.Commands.Add(CreateCommand((int)DiagramCommand.SubmenuOrder, !isConnectorMenu));

      /* Added SubmenuRotateOrFlip in the context menu */
      contextMenu.Commands.Add(CreateCommand((int)DiagramCommand.SubmenuRotateOrFlip, false));

        if (offpageSystems.Count > 0)  //Lets suppose it is list of String and I am creating a Sub menu
        {
          NCommand offpageCommand = new NCommand();
          offpageCommand.Context = new NCommandContext(new NCommandProperties()
          {
           BeginGroup = true,
           ShowArrowStyle = ShowArrowStyle.Default,
           Name = "OffpageParent",
           ID = ((int)DiagramCommand.LastCommandId + 20),
           Text = "Offpage HVAC Systems"
          });         
         
          contextMenu.Commands.Add(offpageCommand);

          for (int cnt = 0; cnt < offpageSystems.Count; cnt++)
          {
           NCommand subCmd = new NCommand(new NCommandProperties()
           {
            Name = string.Format("OffpageChild{0}", cnt),
            ID = ((int)DiagramCommand.LastCommandId + 20 + cnt),
            Text = offpageSystems[cnt].SimModelName,
            Tag = offpageSystems[cnt].RowReference
           });
           offpageCommand.Commands.Add(subCmd);
          }

        }
 
     
      //DiagramCommandRange
    }
    return contextMenu;
   }


Please suggest me the correct way to create context menu items which should work as like 'Action' DiagramCommandRange. How can i create another command range so that these submenu work like them??

Thanks in Advance



By Nevron Support - 7 Years Ago
Hello Niranjan,

When you create the context menu in BuildContextMenu you need to pass the Manger property into the constructor parameters:

contextMenu = new NContextMenu(Manager);


You need to create the offpageCommand as follows:

NCommandContext offpageContext = new NCommandContext();
NCommand offpageCommand = NCommand.FromContext(offpageContext);
Manager.Contexts.Add(offpageContext);

Note that you need to add the command's context to the Manager's context collection.

Children commands need to be created the same way but instead of adding their context to the Manager's Contexts collection you have to add them to the Contexts collection of their parent command:
NCommandContext context = new NCommandContext();
NCommand subCmd = NCommand.FromContext(context);
offpageCommand.Context.Contexts.Add(context);

This way when these custom commands were executed the CommandContextExecuted event should fire.