How to perform nested ContextMenu implementation?

I have this menu like this:

// open context menu
var contextMenu = new ContextMenu();
var versionsMenu = new ToolStripDropDownMenu() {Text = "Version"};
StaticHelpers.GetPackageVersions(textBlockSelected.Text).ForEach(f=> versionsMenu.Items.Add(f));
var scheduleMenu = new ToolStripMenuItem {Text = "Schedule"};
var argumentsMenu = new ToolStripMenuItem() {Text = "Arguments"};
var removeMenu = new ToolStripMenuItem {Text = "Remove"};

//show context menu

//add handlers
// executeMenu.Click += (o, args) => { ExecutePackage(sender); };

//open context menu
contextMenu.IsOpen = true;

I have tried MenuItems instead of ToolStripMenuItem or ToolStripDropDownMenu but could not find any documentation or examples anywhere about how you can make these things nested, for example, when the user right clicks on one of my controls I want to show this menu:

  • Version
  • Schedule
  • Arguments
  • Remove

If the user hovers over version, I want another contextMenu to extend and show the following:

  • V1.0
  • V1.1
  • V1.2

How can I achieve this functionality?

>Solution :

To add a sub-menu, you take an existing item and do the same to it:

var versionsMenu = new ToolStripMenuItem();

Leave a Reply