Menus
- 9 minutes to read
Many DevExpress controls have built-in context-dependent popup menus. You can customize them or replace with custom menus in code. The built-in context menus are encapsulated by DXPopupMenu class objects.
The PopupMenu component allows you to create a non-context-dependent popup menu for a control. Unlike the DXPopupMenu, the PopupMenu component provides design-time customization tools.
The current document covers information on how to customize built-in context-dependent popup menus for the DevExpress controls.
Popup Menu Customization Settings
Specific DevExpress .NET controls contain properties to enable/disable predefined items in their built-in context menus. Below is a list of these controls and corresponding properties:
- Data Grid - See members provided by the GridView.OptionsMenu object.
- Tree List - See members provided by the TreeList.OptionsMenu object.
- PivotGridControl - See members provided by the PivotGridControl.OptionsMenu object.
- VGridControl, PropertyGridControl - See members provided by the VGridControlBase.OptionsMenu object.
- PictureEdit - See PictureEdit.Properties.ShowEditMenuItem, PictureEdit.Properties.ShowCameraMenuItem and PictureEdit.Properties.ShowZoomSubMenu.
Example - Enable Predefined Menu Item
The following code enables the “Show Footer” menu item via the GridView.OptionsMenu.ShowFooterItem property.
Popup Menu Customization Events
DevExpress .NET controls provide events to perform advanced menu customization tasks:
- Customize the order, visibility and text of all predefined menu items.
- Add custom menu items.
- Perform custom actions when a user clicks predefined menu items.
Control | Menu Customization Events |
---|---|
See Popup Menus. | |
See Context Menus. | TreeList.PopupMenuShowing and TreeList.TreeListMenuItemClick |
PivotGridControl.PopupMenuShowing and PivotGridControl.MenuItemClick | |
See Context Menu. | |
Text Editors (TextEdit descendants) |
When you handle any of these events, you can access the currently processed menu from the e.Menu event argument. The e.Menu.Items property returns a collection of menu items, encapsulated by the following objects:
Example - Customize Default Menu Items
The following code handles the GridView.PopupMenuShowing event to change predefined commands in the Data Grid’s Column Header Menu:
- Captions of the “Sort Ascending” and “Sort Descending” commands are replaced with custom strings.
- A few commands are hidden.
- Two “Best Fit” commands are disabled.
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraGrid.Views.Grid;
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if(e.MenuType == GridMenuType.Column) {
string sortAsc = "A to Z";
string sortDesc = "Z to A";
if (e.HitInfo.Column.ColumnType == typeof(DateTime)) {
sortAsc = "Old to New";
sortDesc = "New to Old";
}
DXMenuItem itemAsc = e.Menu.Find(GridStringId.MenuColumnSortAscending); // "Sort Ascending"
if (itemAsc != null)
itemAsc.Caption = sortAsc;
DXMenuItem itemDesc = e.Menu.Find(GridStringId.MenuColumnSortDescending); // "Sort Descending"
if (itemDesc != null)
itemDesc.Caption = sortDesc;
e.Menu.Hide(GridStringId.MenuFooterHide); //"Hide Footer"
e.Menu.Hide(GridStringId.MenuColumnFilterEditor); // "Filter Editor..."
e.Menu.Hide(GridStringId.MenuColumnFindFilterShow); // "Show Find Panel"
e.Menu.Hide(GridStringId.MenuColumnFindFilterHide); // "Hide Find Panel"
e.Menu.Hide(GridStringId.MenuColumnAutoFilterRowHide); // "Hide Auto Filter Row"
e.Menu.Hide(GridStringId.MenuColumnAutoFilterRowShow); // "Show Auto Filter Row"
DXMenuItem itemBestFit = e.Menu.Find(GridStringId.MenuColumnBestFit); // "Best Fit"
if (itemBestFit != null)
itemBestFit.Enabled = false;
DXMenuItem itemBestFitAll = e.Menu.Find(GridStringId.MenuColumnBestFitAllColumns); // "Best Fit All Columns"
if (itemBestFitAll != null)
itemBestFitAll.Enabled = false;
}
}
Example - Add Custom Menu Items
The following example shows how to use the GridView.PopupMenuShowing event to create a custom menu in the Data Grid control. The created menu is displayed when you right-click within a data row or group row. It contains a ‘Rows’ submenu with a single “Delete this row” regular button, and a ‘Cell Merging’ check button.
A click on the ‘Delete this row’ button invokes the ColumnView.DeleteRow method. The ‘Cell Merging’ check button toggles the GridOptionsView.AllowCellMerge option.
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
GridView view = sender as GridView;
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row) {
int rowHandle = e.HitInfo.RowHandle;
// Delete existing menu items, if any.
e.Menu.Items.Clear();
// Add the Rows submenu with the 'Delete Row' command
e.Menu.Items.Add(CreateSubMenuRows(view, rowHandle));
// Add the 'Cell Merging' check menu item.
DXMenuItem item = CreateMenuItemCellMerging(view, rowHandle);
item.BeginGroup = true;
e.Menu.Items.Add(item);
}
}
DXMenuItem CreateSubMenuRows(GridView view, int rowHandle) {
DXSubMenuItem subMenu = new DXSubMenuItem("Rows");
string deleteRowsCommandCaption;
if (view.IsGroupRow(rowHandle))
deleteRowsCommandCaption = "&Delete rows in this group";
else
deleteRowsCommandCaption = "&Delete this row";
DXMenuItem menuItemDeleteRow = new DXMenuItem(deleteRowsCommandCaption, new EventHandler(OnDeleteRowClick), imageCollection1.Images[0]);
menuItemDeleteRow.Tag = new RowInfo(view, rowHandle);
menuItemDeleteRow.Enabled = view.IsDataRow(rowHandle) || view.IsGroupRow(rowHandle);
subMenu.Items.Add(menuItemDeleteRow);
return subMenu;
}
DXMenuCheckItem CreateMenuItemCellMerging(GridView view, int rowHandle) {
DXMenuCheckItem checkItem = new DXMenuCheckItem("Cell &Merging",
view.OptionsView.AllowCellMerge, null, new EventHandler(OnCellMergingClick));
checkItem.Tag = new RowInfo(view, rowHandle);
checkItem.ImageOptions.Image = imageCollection1.Images[1];
return checkItem;
}
void OnDeleteRowClick(object sender, EventArgs e) {
DXMenuItem menuItem = sender as DXMenuItem;
RowInfo ri = menuItem.Tag as RowInfo;
if (ri != null) {
string message = menuItem.Caption.Replace("&", "");
if (XtraMessageBox.Show(message + " ?", "Confirm operation", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
ri.View.DeleteRow(ri.RowHandle);
}
}
void OnCellMergingClick(object sender, EventArgs e) {
DXMenuCheckItem item = sender as DXMenuCheckItem;
RowInfo info = item.Tag as RowInfo;
info.View.OptionsView.AllowCellMerge = item.Checked;
}
class RowInfo {
public RowInfo(GridView view, int rowHandle) {
this.RowHandle = rowHandle;
this.View = view;
}
public GridView View;
public int RowHandle;
}
Tip
You can also create a custom DXPopupMenu menu and assign it to the e.Menu event argument.
Display Modes
A DXPopupMenu object can be displayed as a regular menu, popup bar or RibbonMiniToolbar. To specify the menu’s display mode, set the DXPopupMenu.MenuViewType property to the required enumeration value and use a corresponding menu manager.
When you invoke a DXPopupMenu manually in code, you can specify a menu manager via the popup menu’s IDXDropDownControl.Show method. For built-in menus in DevExpress controls, you can specify a menu manager via a control’s MenuManager property.
The following menu managers are available.
SkinMenuManager - Displays a DXPopupMenu as a regular menu.
Note
SkinMenuManager presents DXEditMenuItem objects as text labels without edit boxes. DXButtonGroupItem objects are displayed as sub-menus.
// Display a DXPopupMenu as a regular menu UserLookAndFeel lf = UserLookAndFeel.Default; Control parentControl = this; Point pt; DXPopupMenu dxPopupMenu = new DXPopupMenu(); //... dxPopupMenu.MenuViewType = MenuViewType.Menu; dxPopupMenu.ShowPopup(parentControl, pt); //or ((IDXDropDownControl)dxPopupMenu).Show(new SkinMenuManager(lf), parentControl, pt);
BarManager - Displays DXPopupMenu as a popup bar.
// Display a DXPopupMenu as a popup bar Control parentControl = this; Point pt; DXPopupMenu dxPopupMenu = new DXPopupMenu(); //... dxPopupMenu.MenuViewType = MenuViewType.Toolbar; dxPopupMenu.ShowPopup(parentControl, pt); //or ((IDXDropDownControl)dxPopupMenu).Show(barManager1, parentControl, pt);
RibbonControl - Displays a DXPopupMenu as a RibbonMiniToolbar object.
// Display a DXPopupMenu as a RibbonMiniToolbar Control parentControl = this; Point pt; DXPopupMenu dxPopupMenu = new DXPopupMenu(); //... dxPopupMenu.MenuViewType = MenuViewType.RibbonMiniToolbar; dxPopupMenu.ShowPopup(parentControl, pt); //or ((IDXDropDownControl)dxPopupMenu).Show(RibbonControl1, parentControl, pt);
Related API
- The inherited IDXDropDownControl.Show method displays a popup menu. You do not need to use this method to open the DevExpress Controls’ built-in menus, as these menus are automatically displayed on a control’s right click.
- The DXPopupMenu.HidePopup method hides a popup menu.
- The DXPopupMenu.CloseUp event fires after the menu has been closed.
- The DXPopupMenu.Alignment property determines a menu’s alignment relative to the mouse cursor. This property is supported only when the DXPopupMenu is displayed as a RibbonMiniToolbar object.
Examples
- How to: Implement Custom Menu in XtraGrid Control
- How to: Add DXButtonGroupItem to DXPopupMenu and Display Menu as RibbonMiniToolbar
- How to: Add DXEditMenuItem to DXPopupMenu and Display Menu as Toolbar
- How to: Display DXPopupMenu as a Regular Menu, Popup Bar or RibbonMiniToolbar