Skip to main content
All docs
V25.2
  • IContextMenuItemCollection.IndexOf(IContextMenuItem) Method

    Returns the specified item’s position within the item collection.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.v25.2.dll

    NuGet Package: DevExpress.Blazor

    Declaration

    int IndexOf(
        IContextMenuItem item
    )

    Parameters

    Name Type Description
    item IContextMenuItem

    Context menu item.

    Returns

    Type Description
    Int32

    The item’s position in the item collection. -1 if the collection does not contain the specified item.

    Remarks

    Handle the CustomizeContextMenu event to modify context commands available in the Grid, Rich Text Editor, or TreeList component. In the event handler, call the IndexOf method to obtain a context menu item’s position.

    Grid Example

    The following code snippet adds the Clear Grouping item after the Ungroup Column command:

    @inject WeatherForecastService ForecastService
    
    <DxGrid Data="@Data"
            ShowGroupPanel="true"
            ContextMenus="GridContextMenus.All"
            CustomizeContextMenu="CustomizeContextMenu">
        <Columns>
            <DxGridDataColumn FieldName="Date" DisplayFormat="D" />
            <DxGridDataColumn FieldName="TemperatureC" Caption="@("Temp. (\x2103)")" Width="120px" />
            <DxGridDataColumn FieldName="TemperatureF" Caption="@("Temp. (\x2109)")" Width="120px" />
            <DxGridDataColumn FieldName="Forecast" GroupIndex="0" />
            <DxGridDataColumn FieldName="CloudCover" />
        </Columns>
    </DxGrid>
    
    @code {
        object Data { get; set; }
    
        protected override void OnInitialized() {
            Data = ForecastService.GetForecast();
        }
        void CustomizeContextMenu(GridCustomizeContextMenuEventArgs args) {
            if (args.Context is GridHeaderCommandContext) {
                var ungroupItem = args.Items[GridContextMenuDefaultItemNames.UngroupColumn];
                if (ungroupItem != null) {
                    var position = args.Items.IndexOf(ungroupItem);
                    var newItem = args.Items.Add(position, GridContextMenuDefaultItemNames.ClearGrouping);
                    newItem.BeginGroup = true;
                    ungroupItem.BeginGroup = false;
                }
            }
        }
    }
    

    Rich Text Editor Example

    The following code snippet adds a custom Show URL item before the Open Hyperlink item if a hyperlink is selected:

    <DxRichEdit CustomizeContextMenu=OnCustomizeContextMenu />
    
    @code {
        DxPopup popup;
        Selection selection;
        string link;
        // ...
        async Task OnCustomizeContextMenu(IContextMenuItemCollection items) {
            var hyperlinks = await selection.ActiveSubDocument.Hyperlinks.GetAllAsync();
            var hyperlink = hyperlinks.SingleOrDefault(h => Enumerable.Range(h.Interval.Start, h.Interval.Length).Contains(selection.CaretPosition));
            link = hyperlink?.Url;
            if (!string.IsNullOrEmpty(link)) {
                var openHyperlinkItem = items[RichEditContextMenuItemNames.OpenHyperlink];
                openHyperlinkItem.BeginGroup = false;
                var index = items.IndexOf(openHyperlinkItem);
                var showURLItem = items.AddCustomItem(index, "Show URL");
                showURLItem.Click = async () => {
                    if (popup is not null)
                        await popup.ShowAsync();
                };
                showURLItem.BeginGroup = true;
            }
        }
    }
    

    Run Demo: Context Menu Customization

    TreeList Example

    The following code sample adds Expand All/Collapse All items to the header context menu:

    @inject EmployeeTaskService EmployeeTaskService
    
    <DxTreeList Data="TreeListData"
                KeyFieldName="Id"
                ParentKeyFieldName="ParentId"
                ContextMenus="TreeListContextMenus.Header"
                CustomizeContextMenu="CustomizeContextMenu">
        <Columns>
            <DxTreeListDataColumn FieldName="Name" Caption="Task" />
            <DxTreeListDataColumn FieldName="EmployeeName" />
            <DxTreeListDataColumn FieldName="StartDate" />
            <DxTreeListDataColumn FieldName="DueDate" />
        </Columns>
    </DxTreeList>
    
    @code {
        List<EmployeeTask> TreeListData { get; set; }
    
        protected override void OnInitialized() {
            TreeListData = EmployeeTaskService.GenerateData();
        }
        void CustomizeContextMenu(TreeListCustomizeContextMenuEventArgs args) {
             var newItem = args.Items.Add(TreeListContextMenuDefaultItemNames.CollapseAll);
             var itemPosition = args.Items.IndexOf(newItem);
             args.Items.Add(itemPosition, TreeListContextMenuDefaultItemNames.ExpandAll).BeginGroup = true;
        }
    }
    
    See Also