Skip to main content
A newer version of this page is available. .

How to: Modify, Add or Remove Items in a Context Menu

  • 3 minutes to read

This example demonstrates how to customize the SpreadsheetControl’s context menu - remove the existing menu items and add new items.

Handle the SpreadsheetControl.PopupMenuShowing event. Use the PopupMenuShowingEventArgs.MenuType property to determine the visual element for which the popup menu is invoked.

To remove a menu item, create the RemoveSpreadsheetCommandAction object, set its ID to the ID of the command to remove and add that object to the PopupMenuShowingEventArgs.Customizations collection.

To add a new menu item, create a new BarButtonItem and add it to the PopupMenuShowingEventArgs.Customizations collection.

<dxb:BarManager x:Name="barManager1" ToolbarGlyphSize="Small">
    <dxb:BarManager.Items>
        <dxb:BarButtonItem
            x:Name="customNewFile"
            Command="{Binding FileNew, Mode=OneTime, Source={StaticResource commands}}"
            Content="Custom Create New File" />
        <dxb:BarButtonItem
            x:Name="customMergeCells"
            Command="{Binding EditingMergeAndCenterCells, Mode=OneTime, Source={StaticResource commands}}"
            Content="Custom Merge Cells" />
    </dxb:BarManager.Items>
    <Grid>
        <dxsps:SpreadsheetControl
            x:Name="spreadsheetControl1"
            BarManager="{Binding ElementName=barManager1, Mode=OneTime}"
            PopupMenuShowing="SpreadsheetControlPopupMenuShowing">
            <dxsps:SpreadsheetControl.MenuCustomizations>
                <dxsps:SpreadsheetMenuCustomization MenuType="Cell">
                    <dxb:BarButtonItemLink dxb:BarItemLinkActionBase.ItemLinkIndex="0">
                        <dxb:BarButtonItemLink.BarItemName>
                            customMergeCells
                        </dxb:BarButtonItemLink.BarItemName>
                    </dxb:BarButtonItemLink>
                </dxsps:SpreadsheetMenuCustomization>
                <dxsps:SpreadsheetMenuCustomization>
                    <dxb:BarButtonItemLink dxb:BarItemLinkActionBase.ItemLinkIndex="1">
                        <dxb:BarButtonItemLink.BarItemName>
                            customNewFile
                        </dxb:BarButtonItemLink.BarItemName>
                    </dxb:BarButtonItemLink>
                </dxsps:SpreadsheetMenuCustomization>
                <dxsps:SpreadsheetMenuCustomization>
                    <dxb:BarItemLinkSeparator dxb:BarItemLinkActionBase.ItemLinkIndex="2" />
                </dxsps:SpreadsheetMenuCustomization>
                <dxsps:SpreadsheetMenuCustomization MenuType="ColumnHeading">
                    <dxsps:RemoveSpreadsheetCommandAction 
                        Id="{x:Static dxspscore:SpreadsheetCommandId.HideColumnsContextMenuItem}" />
                    <dxsps:RemoveSpreadsheetCommandAction 
                        Id="{x:Static dxspscore:SpreadsheetCommandId.UnhideColumnsContextMenuItem}" />
                </dxsps:SpreadsheetMenuCustomization>
            </dxsps:SpreadsheetControl.MenuCustomizations>
        </dxsps:SpreadsheetControl>
    </Grid>
</dxb:BarManager>