Skip to main content

Customize the Spreadsheet Context Menus

  • 4 minutes to read

This topic describes how to add, modify, or remove items from the WPF Spreadsheet control’s context menus.

Method 1. Use the Menu Customization Actions

To modify the Spreadsheet control’s context menus, add the required bar actions to the SpreadsheetControl.MenuCustomizations collection. Use the SpreadsheetMenuCustomization.MenuType property to specify the menu type you want to customize.

View Example

Add Items

To create a new menu item, add an InsertAction to the SpreadsheetControl.MenuCustomizations collection.

The code snippet below adds the following custom items to the Cell context menu:

  • A menu item separator
  • A Highlight Cells item that highlights selected cells in yellow
  • A Merge Cells item that is bound to the MergeAndCenterCells spreadsheet command

Custom context menu for the Spreadsheet control

<!--Add the following namespace declarations:
xmlns:dxsps="http://schemas.devexpress.com/winfx/2008/xaml/spreadsheet"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"-->

<dxsps:SpreadsheetControl x:Name="spreadsheetControl">
    <dxsps:SpreadsheetControl.MenuCustomizations>
        <dxsps:SpreadsheetMenuCustomization MenuType="Cell">
            <!--Add a separator after the first three menu items.-->
            <dxb:InsertAction Index="3">
                <dxb:BarItemSeparator />
            </dxb:InsertAction>
            <!--Insert a custom menu item under the separator
            to highlight selected cells.-->
            <dxb:InsertAction Index="4">
                <dxb:BarButtonItem Content="Highlight Cells" 
                                   ItemClick="HighlightCells_ItemClick" />
            </dxb:InsertAction>
            <!--Add a new item to the end of the context menu
            and bind this item to the spreadsheet command.-->
            <dxb:BarButtonItem Command="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay, 
                Path=(dxsps:SpreadsheetControl.Spreadsheet).CommandProvider.MergeAndCenterCells}" 
                Content="Merge Cells" />
        </dxsps:SpreadsheetMenuCustomization>
    </dxsps:SpreadsheetControl.MenuCustomizations>
</dxsps:SpreadsheetControl>

Handle the Highlight Cells item’s ItemClick event as shown below.

private void HighlightCells_ItemClick(object sender, ItemClickEventArgs e) {
    // Fill selected cells with the yellow color. 
    spreadsheetControl.Selection.FillColor = System.Drawing.Color.Yellow;
}

Modify Items

To change an existing menu item, add an UpdateAction to the SpreadsheetControl.MenuCustomizations collection. Specify the item name, the property you want to modify, and the new property value. Item names are the DevExpress.Xpf.Spreadsheet.DefaultBarItemNames class fields that start with “PopupMenuItem_”.

The following code snippet changes the Copy item’s caption to Copy Cells:

<!--Add the following namespace declarations:
xmlns:dxsps="http://schemas.devexpress.com/winfx/2008/xaml/spreadsheet"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"-->

<dxsps:SpreadsheetControl x:Name="spreadsheetControl">
    <dxsps:SpreadsheetControl.MenuCustomizations>
        <dxsps:SpreadsheetMenuCustomization MenuType="Cell">
            <!--Change the "Copy" item's content.-->
            <dxb:UpdateAction ElementName="{x:Static dxsps:DefaultBarItemNames.PopupMenuItem_Copy}" 
                              PropertyName="Content" 
                              Value="Copy Cells" />
        </dxsps:SpreadsheetMenuCustomization>
    </dxsps:SpreadsheetControl.MenuCustomizations>
</dxsps:SpreadsheetControl>

Remove Items

To delete an item from the menu, add a RemoveAction to the SpreadsheetControl.MenuCustomizations collection and specify the item name. Item names are the DevExpress.Xpf.Spreadsheet.DefaultBarItemNames class fields that start with “PopupMenuItem_”.

The following code snippet removes two items from the Cell menu:

<!--Add the following namespace declarations:
xmlns:dxsps="http://schemas.devexpress.com/winfx/2008/xaml/spreadsheet"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"-->

<dxsps:SpreadsheetControl x:Name="spreadsheetControl">
    <dxsps:SpreadsheetControl.MenuCustomizations>
       <dxsps:SpreadsheetMenuCustomization MenuType="Cell">
          <!--Remove the "Insert Comment" item from the menu.-->
          <dxb:RemoveAction ElementName="{x:Static dxsps:DefaultBarItemNames.PopupMenuItem_InsertComment}"/>
          <!--Remove the "Hyperlink" item from the menu.-->
          <dxb:RemoveAction ElementName="{x:Static dxsps:DefaultBarItemNames.PopupMenuItem_InsertHyperlink}"/>
       </dxsps:SpreadsheetMenuCustomization>
    </dxsps:SpreadsheetControl.MenuCustomizations>
</dxsps:SpreadsheetControl>

Method 2. Handle the PopupMenuShowing Event

Handle the SpreadsheetControl.PopupMenuShowing event to customize the Spreadsheet control’s context menus in code-behind. The PopupMenuShowingEventArgs.MenuType argument allows you to determine the menu type for which the event is raised.

Use the PopupMenuShowingEventArgs.Customizations property to add or remove menu items.

The example below customizes the Cell context menu as follows:

  • Creates new Merge Cells and Highlight Cells items
  • Removes the Insert Comment and Hyperlink items

Custom context menu for the Spreadsheet control

<dxsps:SpreadsheetControl x:Name="spreadsheetControl" 
                          PopupMenuShowing="spreadsheetControl_PopupMenuShowing"/>
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Spreadsheet;
using DevExpress.Xpf.Spreadsheet.Menu;
using DevExpress.XtraSpreadsheet.Commands;
// ... 

private void spreadsheetControl_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    // Check whether the event is raised for a spreadsheet cell.
    if (e.MenuType == SpreadsheetMenuType.Cell) {

        // Create a menu item to merge selected cells
        // and bind this item to the spreadsheet command.
        e.Customizations.Add(new BarButtonItem() {
            Command = SpreadsheetUICommand.EditingMergeAndCenterCells,
            Content = "Merge Cells",
            CommandParameter = spreadsheetControl
        });

        // Create a custom menu item to highlight selected cells.
        var menuItem = new BarButtonItem();
        menuItem.Name = "highlightCellsItem";
        menuItem.Content = "Highlight Cells";
        menuItem.ItemClick += MenuItem_ItemClick;
        e.Customizations.Add(menuItem);
    }

    // Remove the "Insert Comment" item from the menu.
    e.Customizations.Add(new RemoveSpreadsheetCommandAction() {
        Id = SpreadsheetCommandId.ReviewInsertCommentContextMenuItem
    });

    // Remove the "Hyperlink" item from the menu.
    e.Customizations.Add(new RemoveSpreadsheetCommandAction() {
        Id = SpreadsheetCommandId.InsertHyperlinkContextMenuItem
    });
}

private void MenuItem_ItemClick(object sender, ItemClickEventArgs e) {
    // Fill selected cells with the yellow color.
    spreadsheetControl.Selection.FillColor = System.Drawing.Color.Yellow;
}
See Also