How To Determine which List Trigger Context Menu in Blazor ListBox?
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 Blazor ListBox feature tour page
The SfListBox component is configured with a data source and event handlers, using 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
I 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 feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor ListBox example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries 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!