Ribbon provides additional commands for each ribbon item through context menu. To access Ribbon item's context menu, right-click on the ribbon item and the context menu appears as following:
The following table demonstrates the commands provided by RibbonItem context menu:
Commands | Descriptions |
---|---|
Add to Quick Access Toolbar | Adds the item to Quick Access Toolbar. |
Minimize the Ribbon | Minimizes the Ribbon |
Ribbon provides access to some basic operations through the context menu. However, there might be a scenario where you would want to access custom operations via context menu. For instance, you want to facilitate end-users to hide ribbon items as per their requirement using the context menu as shown in the following image.
To display a customized context menu for the ribbon item, follow these steps:
C# |
Copy Code
|
---|---|
RibbonButton button1 = new RibbonButton(); button1.Text = "Hide"; //Adding Custom Item to Ribbon ContextMenu c1Ribbon1.ContextMenuItems.Add(button1); |
C# |
Copy Code
|
---|---|
c1Ribbon1.MouseUp += C1Ribbon1_MouseUp; |
C# |
Copy Code
|
---|---|
private void C1Ribbon1_MouseUp(object? sender, MouseEventArgs e) { if (e.Button != MouseButtons.Right) return; var rItem = c1Ribbon1.GetItemAt(e.Location); if (rItem != null && rItem is RibbonItem ri) { c1Ribbon1.ShowContextMenu(this.PointToScreen(e.Location), ri); } } |
C# |
Copy Code
|
---|---|
c1Ribbon1.ContextMenuPopUp += C1Ribbon1_ContextMenuPopUp; |
C# |
Copy Code
|
---|---|
private void C1Ribbon1_ContextMenuPopup(object? sender, C1.Win.Ribbon.ContextMenuPopupEventArgs e) { //If the ContextMenu is opening for ribbonButton2 if(e.Component is RibbonButton ribbonButton2 && ribbonButton2.Name == "GenderBtn") { //Use Custom Menu instead of default menu, items for which are added above e.UseCustomMenu = true; } } |
Alternatively, you can use RibbonMenuItems Collection Editor to add custom items to the context menu. To access RibbonMenuItems Collection Editor, click the ellipsis next to the ContextMenuItems property from the Properties Window. The RibbonMenuItems Collection Editor appears as following:
In RibbonMenuItems Collection Editor, the right pane displays properties for each defined ribbon menu item. The left pane displays a list of menu items along with Add and Remove buttons which can be used to add and remove menu items, respectively.