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# |
コードのコピー
|
---|---|
RibbonButton button1 = new RibbonButton(); button1.Text = "Hide"; //リボンの ContextMenu にカスタム項目を追加します。 c1Ribbon1.ContextMenuItems.Add(button1); |
C# |
コードのコピー
|
---|---|
c1Ribbon1.MouseUp += C1Ribbon1_MouseUp; |
C# |
コードのコピー
|
---|---|
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# |
コードのコピー
|
---|---|
c1Ribbon1.ContextMenuPopUp += C1Ribbon1_ContextMenuPopUp; |
C# |
コードのコピー
|
---|---|
private void C1Ribbon1_ContextMenuPopup(object? sender, C1.Win.Ribbon.ContextMenuPopupEventArgs e) { //ContextMenu が ribbonButton2 に対して開いている場合。 if(e.Component is RibbonButton ribbonButton2 && ribbonButton2.Name == "GenderBtn") { //上記で追加されたデフォルト メニューの代わりに、カスタム メニューを使用します。 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.