Articles in this section
Category / Section

How To Determine which List Trigger Context Menu in Blazor ListBox?

3 mins read

In a Blazor application, you may want to identify which list items in a ListBox trigger the opening of a context menu. This can be achieved by utilizing the HtmlAttributes property of ListBoxFieldSettings to assign unique IDs to each list item. Additionally, the Opened event of the context menu can be used to capture the ID of the list item that was interacted with. For more information, visit the Blazor ListBox feature tour page.

The SfListBox component is configured with a data source and event handlers, using the HtmlAttributes property to assign unique IDs. The SfContextMenu, linked to the ListBox, provides options like “Add Column” and “Delete Column.” The Opened event identifies the selected list item via args.TargetId and retrieves its data using the GetDataByValue method.

Below is a code snippet demonstrating how to implement the above steps:

<SfListBox id="target" @ref="@ListboxObj" CssClass="e-listbox" TValue="string[]" DataSource="@FormItems" TItem="FormItem" AllowDragAndDrop="true">
    <ListBoxEvents TValue="string[]" ValueChange="ItemChanged" TItem="FormItem"></ListBoxEvents>
    <ListBoxFieldSettings Text="Field" Value="FormItemNo" HtmlAttributes="attributes" />
    
    <SfContextMenu Target="#target" TValue="MenuItem">
        <MenuItems>
            <MenuItem Text="Add column"></MenuItem>
            <MenuItem Text="Delete column"></MenuItem>
        </MenuItems>
        <MenuEvents TValue="MenuItem" ItemSelected="@selectedHandler" Opened="open"></MenuEvents>
    </SfContextMenu>
</SfListBox>

@code {
    private List<FormItem> FormItems = new List<FormItem>
    {
        new FormItem { Field = "Field1", FormItemNo = "1", attributes = new Dictionary<string, object>() {{ "id", "1" }}},
        new FormItem { Field = "Field2", FormItemNo = "2", attributes = new Dictionary<string, object>() {{ "id", "2" }} },
        new FormItem { Field = "Field3", FormItemNo = "3", attributes = new Dictionary<string, object>() {{ "id", "3" }} },
    };

    private void open(OpenCloseMenuEventArgs<MenuItem> args)
    {
        selectedList = ListboxObj.GetDataByValue(new string[] { args.TargetId });
    }
}

Sample: https://blazorplayground.syncfusion.com/LjrfirVQBQllTACK

Conclusion
We hope you enjoyed learning about how to determine which list trigger context menu in Blazor ListBox.

You can refer to our Blazor ListBox feature tour page to know about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor ListBox example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor ListBox and other Blazor components.

If you have any questions or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied