Skip to main content
All docs
V24.2

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

IContextMenuItemCollection.IndexOf(IContextMenuItem) Method

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

Namespace: DevExpress.Blazor.Office

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

#Declaration

C#
int IndexOf(
    IContextMenuItem menuItem
)

#Parameters

Name Type Description
menuItem IContextMenuItem

The 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

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

Razor
<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

See Also