Skip to main content

How to Add Custom Items into Built-in Context Menus

  • 3 minutes to read

This topic provides an example of adding items to built-in context menus, and handling clicks on these items.

The pivot grid allows you to display built-in or custom context menus for field headers, grouping values, and header areas. To determine the menu type to be displayed, use the UseBuiltInMenu property available via the pivot grid’s PopupMenus.FieldHeaderMenu, PopupMenus.GroupValueMenu, and PopupMenus.HeaderAreaMenu properties, respectively. To display a built-in context menu for the corresponding element, set the UseBuiltInMenu property to True, and use the menu’s Items property to specify default items to be added into this menu. In addition to default items, you can populate these menus with custom items by handling the pivot grid’s PopupMenus.OnPopup event. To respond to clicks on custom items, handle the pivot grid’s PopupMenus.OnClick event.

The following code snippet demonstrates how to add a separator and an item to a field header context menu, and in response to a click, collapse all the columns or rows that correspond to the field header whose menu is invoked.

type
  TcxPivotGridCustomPopupMenuAccess = class(TcxPivotGridCustomPopupMenu);
const
  customPopupMenuItem = 100;  // A constant that identifies a custom menu item
procedure <TForm>.DBPivotGridPopupMenusPopup(
  Sender: TcxCustomPivotGrid; ABuiltInMenu: TcxPivotGridCustomPopupMenu;
  var AHandled: Boolean);
begin
  // Adding items to a field header context menu
  if ABuiltInMenu is TcxPivotGridFieldHeaderMenu then
  begin
    with TcxPivotGridCustomPopupMenuAccess(ABuiltInMenu) do
    begin
      // Adding a separator to the root of a field header context menu
      CreateSeparator(Root);
      // Adding a menu item with the specified Tag to the menu's root
      CreateSubItem(Root, 'Custom Menu Item', customPopupMenuItem, True);
    end;
  end;
end;
procedure <TForm>.DBPivotGridPopupMenusClick(
  Sender: TcxCustomPivotGrid; AItem: TMenuItem; var AHandled: Boolean);
begin
  // Handling a click on the added menu item
  if (Sender.HitTest.HitAtField) and (AItem.Tag = customPopupMenuItem) then
    TcxPivotGridField(Sender.HitTest.Field).CollapseAll;
end;

Note

The AHandled parameter is not changed within the PopupMenus.OnPopup event handler, so that the modified context menu can be displayed.

See Also