Nevron Open Vision Documentation
Diagram / Commanding / Context Menu
In This Topic
    Context Menu
    In This Topic

    NOV Diagram shows a context menu with different items based on the diagram item the user right-clicked. The context menu is accessible via the ContextMenu property of the NDrawingView class.

     Disabling the Context Menu

    If you don't wont to show the context menu for specific diagram elements, set any of the following properties of the NDrawingContextMenu to false:

    • ShowPageMenu - determines whether to show a context menu when the user right-clicks an empty area of the page.
    • ShowShapeMenu - determines whether to show a context menu when the user right-clicks a shape.
    • ShowGuidelineMenu - determines whether to show a context menu when the user right-clicks a guideline.
    • ShowMiniToolbar - determines whether the mini toolbar should be displayed.

    If you want to hide the context menu for specific elements, create a class that inheirts NDrawingContextMenu and override the CanShowContextMenu method:

    Hide the context menu for groups
    Copy Code
    public class CustomContextMenu : NDrawingContextMenu
    {
        public CustomContextMenu()
        {
        }
        static CustomContextMenu()
        {
            CustomContextMenuSchema = NSchema.Create(typeof(CustomContextMenu), NDrawingContextMenuSchema);
        }
    
        protected override bool CanShowContextMenu(NElement element)
        {
            if (!base.CanShowContextMenu(element))
                return false;
    
            // TODO: The code below doesn't show the context menu for groups.
            // Replace it with your logic.
            if (element is NGroup)
                return false;
    
            return true;
        }
    
        public static readonly NSchema CustomContextMenuSchema;
    }
    
     Customizing the Context Menu

    NOV Diagram lets you fully customize the context menu shown when the user right clicks somewhere in the drawing view. To do so, you should create a class that inherits NDrawingContextMenu and override the methods you are interested in. Most commonly you will need to override one or both of the following methods:

    • CreateElementSpecificCommands - creates context menu commands for a given element and adds them to the specified menu. The default implementation of this method checks whether the given element implements the INAppendContextMenuItems interface and if it does, calls its AppendContextMenuItems method. You should override this method to add context menu items specific for a given element.
    • BuildContextMenu - creates context menu commands for a given element. You should override this method if you want to customize the whole context menu for the given element and not only to add items. In this method you can call the CreateEditCommands, CreateViewCommands and CreateSelectCommands methods to add the corresponding menu items to the context menu.
    • CreateCustomCommands - creates custom context menu commands.

    You can also override any of the following methods to further modify the context menu:

    • CreateEditCommands - the base implementation creates the default edit commands - Cut/Copy/Paste/Delete. If you override this method and don't call the base implementation, the edit commands won't be created.
    • CreateViewCommands - the base implementation creates a View menu item that contains Show, Zoom and Snap commands. If you override this method and don't call the base implementation, the View menu item won't be created.
    • CreateSelectCommands - the base implementation creates the Select menu item. If you override this method and don't call the base implementation, the Select menu item won't be created.

    The code example below demonstrates how to create a drawing context menu that adds a custom command at the bottom of the context menu:

    Create custom diagram context menu
    Copy Code
    public class CustomContextMenu : NDrawingContextMenu
    {
        public CustomContextMenu()
        {
        }
        static CustomContextMenu()
        {
            CustomContextMenuSchema = NSchema.Create(typeof(CustomContextMenu), NDrawingContextMenuSchema);
        }
    
        protected override void CreateCustomCommands(NMenu menu, NContextMenuBuilder builder)
        {
            base.CreateCustomCommands(menu, builder);
    
            // Add a custom command
            builder.AddMenuItem(menu, NResources.Image_Ribbon_16x16_smiley_png, CustomCommand);
        }
    
        public static readonly NSchema CustomContextMenuSchema;
    }
    
     Assigning a Context Menu to a Drawing View

    When you've created your custom diagram context menu, you should create an instance of it and assign it to the ContextMenu property of the drawing view:

    Assign a diagram context menu to a drawing view
    Copy Code
    drawingView.ContextMenu = new CustomContextMenu();
    
    See Also