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

    Returns the position of the item with the specified name within the item collection.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.v25.2.dll

    NuGet Package: DevExpress.Blazor

    Declaration

    int IndexOf(
        string name
    )

    Parameters

    Name Type Description
    name String

    Item name.

    Returns

    Type Description
    Int32

    The item’s position in the item collection. -1 if the collection does not contain a 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 groupItemPosition = args.Items.IndexOf(GridContextMenuDefaultItemNames.UngroupColumn);
                if (groupItemPosition != -1)
                    args.Items.Add(groupItemPosition + 1, GridContextMenuDefaultItemNames.ClearGrouping);
            }     
        }
    }
    

    Rich Text Editor Example

    The following code snippet adds the Select All item after the Paste command:

    <DxRichEdit CustomizeContextMenu=OnCustomizeContextMenu />
    
    @code {
        void OnCustomizeContextMenu(IContextMenuItemCollection items) {
            items.Remove(RichEditContextMenuItemNames.SelectAll);
            var position = items.IndexOf(RichEditContextMenuItemNames.Paste);
            var newItem = items.Add(position + 1, RichEditContextMenuItemNames.SelectAll);
        }
    }
    

    TreeList Example

    The following code sample adds Expand All/Collapse All items before the Hide Column command:

    @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 hideColumnPosition = args.Items.IndexOf(TreeListContextMenuDefaultItemNames.HideColumn);
            if (hideColumnPosition != -1) {
                args.Items.Add(hideColumnPosition, TreeListContextMenuDefaultItemNames.CollapseAll);
                var expandAllItem = args.Items.Add(hideColumnPosition, TreeListContextMenuDefaultItemNames.ExpandAll);
                expandAllItem.BeginGroup = true;
            }
        }
    }
    
    See Also