PopupMenu Class
The popup menu, managed by a BarManager or RibbonControl.
Namespace: DevExpress.XtraBars
Assembly: DevExpress.XtraBars.v24.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
public class PopupMenu :
PopupMenuBase,
IHasRibbonKeyTipManager,
PopupControl,
IDXDropDownControlEx,
IDXDropDownControl,
IAppearanceOwner,
IOptionsMultiColumnOwner
Remarks
The PopupMenu
can be used to provide easy access to frequently used commands for a specific control. When associated with an object, the popup menu is opened by right-clicking the object. The popup menu can also be displayed in code, using the PopupMenu.ShowPopup method.
The PopupMenu
needs to be associated with a BarManager or RibbonControl. If your project does not contain these components, add one of them to the form. At design time, the PopupMenu
is automatically associated with a BarManager or RibbonControl if they are present on the form. When creating a PopupMenu
in code, ensure that the PopupMenuBase.Manager or PopupMenuBase.Ribbon property refers to an existing BarManager or RibbonControl component.
Design-Time Menu Customization
At design time, the PopupMenu
can be customized by clicking the menu’s smart tag and selecting Run Designer.
A click on this link opens the Popup Menu Editor (different editors are used when the menu is associated with the BarManager and RibbonControl).
If the PopupMenu
is associated with the BarManager, the Bar Manager’s Customization window and a standalone Popup Menu Editor are invoked.
You can customize the menu in a number of ways.
- Dragging commands from the Customization window to the Popup Menu Editor.
- Clicking the [Add] button in the Popup Menu Editor.
- Using a context menu provided by the popup menu’s items.
- Selecting the popup menu’s item and changing its settings in the Visual Studio Properties grid.
If the PopupMenu
is associated with a RibbonControl, the Sub Menus and Popup Menus page of the Ribbon Control’s Designer is opened, providing menu customization tools.
The designer allows you to customize the menu by:
- Dragging commands from the command list (displayed in the center).
- Clicking the [Add] button displayed below all popup menu items.
- Selecting the popup menu’s item and changing its settings in the Properties grid.
Menu Items
Items in popup menus are represented by bar item objects. The PopupMenu
supports the display of any bar item that is provided by the DevExpress Ribbon, Menu and Docking Library: buttons, sub-menus, editors, static text, etc.
For more information, see the Bar Item Links and The List of Bar Items and Links topics.
To populate a popup menu with items in code, add a specific bar item (a BarItem descendant) to the PopupMenuBase.ItemLinks collection.
Sub-menu Show Mode
You can use the BarManager.PopupShowMode and RibbonControl.PopupShowMode properties to specify how popup menus display their sub-menus. Sub-menus can be displayed in the traditional cascaded style as shown in the figure below.
Another option is to display sub-menus in-place of their parent popup menus. In this mode, when an end-user hovers over a sub-menu button with the mouse pointer, the currently opened menu hides and in its place, the required sub-menu is displayed. This mode is more convenient for application designed for tablet devices, as it allows you to save valuable screen space. The figure below shows how the sub-menu is displayed in the in-place mode.
If the in-place mode is enabled, sub-menus can display navigation headers that allow an end-user to navigate backward. See the PopupMenu.ShowNavigationHeader setting for details.
By default, sub-menus are displayed in the traditional cascaded way when managed by the BarManager component and in all styles of the RibbonControl control, except for the TabletOffice and OfficeUniversal styles. In these styles, sub-menus are displayed in the in-place mode by default.
Associating Menu with Controls
An existing PopupMenus can be assigned to the BarButtonItem.DropDownControl or DropDownButton.DropDownControl property. In this instance, the PopupMenu
appears when an appropriate BarButtonItem or DropDownButton‘s drop-down arrow is clicked.
When a BarManager or RibbonControl is present on the form, all controls publish the PopupContextMenu property at design time (its caption in the Properties window looks like ‘PopupContextMenu on barManager1’ or ‘PopupContextMenu on ribbonControl1’). You can use this property to assign a PopupMenu
to this control. This menu will then be displayed when right-clicking on the control at runtime.
To assign a PopupMenu
to a control in code, use the BarManager.SetPopupContextMenu method.
Note
Specific complex controls such as the Grid Control and Tree List do not publish the PopupContextMenu
property, as these controls support different context menus for different visual elements. See the help documentation on these products, to learn how to work with context menus in these controls.
Example: Create a Sub Menu
Before you begin:
- Drop the BarManager component onto the form.
- Drop the SvgImageCollection component onto the form.
- Add the following DevExpress SVG icons to the image collection: Copy, Paste, Add File, Folder.
The following example demonstrates how to create a popup menu and bind the menu to the Form. Right-click the form to invoke the menu (at runtime).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
namespace DevExpressPopupMenu {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
// Completes the BarManager's initialization (to allow its further customization on the form load)
barManager1.ForceInitialize();
// Creates a popup menu.
PopupMenu menu = new PopupMenu();
menu.Manager = barManager1;
// Specifies a collection of vector (SVG) icons.
barManager1.Images = svgImageCollection1;
// Creates and initializes the items.
BarSubItem itemAdd = new BarSubItem(barManager1, "Add");
BarButtonItem itemAddFile = new BarButtonItem(barManager1, "File", 2);
BarButtonItem itemAddFolder = new BarButtonItem(barManager1, "Folder", 3);
itemAdd.AddItems(new BarItem[] { itemAddFile, itemAddFolder });
BarButtonItem itemCopy = new BarButtonItem(barManager1, "Copy", 0);
BarButtonItem itemPaste = new BarButtonItem(barManager1, "Paste", 1);
// Adds the items to the popup menu.
menu.AddItems(new BarItem[] { itemAdd, itemCopy, itemPaste });
// Creates a separator before the Copy item.
itemCopy.Links[0].BeginGroup = true;
// Attaches the popup menu to the form.
barManager1.SetPopupContextMenu(this, menu);
// Subscribes to the 'ItemClick' event handler to process item clicks.
barManager1.ItemClick += BarManager1_ItemClick;
}
void BarManager1_ItemClick(object sender, ItemClickEventArgs e) {
XtraMessageBox.Show(string.Format("The '{0}' item was clicked.", e.Item.Caption));
}
}
}
The image below shows the result.
Example: Show Custom Menu in Grid Control
The following code sample invokes a custom context menu when a user right-clicks a column header:
Add a BarManager to the form and create a custom
PopupMenu
.Handle the GridView.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default header menu.
void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == GridMenuType.Column) {
popupMenu_Column.Tag = e.HitInfo;
popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}";
e.ShowCustomMenu(popupMenu_Column);
}
}
GridHitInfo GetHitInfo(BarItemLink link) {
PopupMenu menu = link.LinkedObject as PopupMenu;
return menu.Tag as GridHitInfo;
}
void barButtonItem_Filter_ItemClick(object sender, ItemClickEventArgs e) {
GridHitInfo info = GetHitInfo(e.Link);
info.View.ShowFilterEditor(info.Column);
}
void barButtonItem_ColumnChooser_ItemClick(object sender, ItemClickEventArgs e) {
GridHitInfo info = GetHitInfo(e.Link);
info.View.ShowCustomization();
}
Example: Show a Popup Menu when a User Right Clicks
The following code displays a specific PopupMenu
if the right mouse button is pressed:
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if(e.Button == MouseButtons.Right)
popupMenu1.ShowPopup(Control.MousePosition);
}