How to Render the Blazor Button Component Using Render Fragment?
The Blazor Dropdown Menu component can be rendered using the render fragment approach, which allows for a flexible and dynamic way to define the content of the Dropdown Menu. This article provides a step-by-step guide on how to implement a Dropdown Button with menu items using the Syncfusion Blazor Button components.
Implementation
To create a Dropdown Button with DropDownMenuItems and its child DropDownMenuItem components, you need to pass them as a ChildContent attribute. Below is an example of how to achieve this:
Code Example
@using Syncfusion.Blazor.SplitButtons
@code {
private RenderFragment? DropdownFragment => builder =>
{
builder.OpenComponent<SfDropDownButton>(0);
builder.AddAttribute(1, "Content", "Export");
builder.AddAttribute(2, "CssClass", "e-inherit");
builder.AddAttribute(3, "ChildContent", (RenderFragment)(menuBuilder =>
{
menuBuilder.OpenComponent<DropDownMenuItems>(4);
menuBuilder.AddAttribute(5, "ChildContent", (RenderFragment)(itemsBuilder =>
{
itemsBuilder.OpenComponent<DropDownMenuItem>(6);
itemsBuilder.AddAttribute(7, "Text", "Excel");
itemsBuilder.AddAttribute(8, "IconClass", "e-icons e-export-excel");
itemsBuilder.CloseComponent();
itemsBuilder.OpenComponent<DropDownMenuItem>(9);
itemsBuilder.AddAttribute(10, "Text", "CSV");
itemsBuilder.AddAttribute(11, "IconClass", "e-icons e-export-csv");
itemsBuilder.CloseComponent();
itemsBuilder.OpenComponent<DropDownMenuItem>(12);
itemsBuilder.AddAttribute(13, "Text", "PDF");
itemsBuilder.AddAttribute(14, "IconClass", "e-icons e-export-pdf");
itemsBuilder.CloseComponent();
}));
menuBuilder.CloseComponent(); // Closing DropDownMenuItems
}));
builder.CloseComponent(); // SfDropDownButton
};
}
<div>
<h3>Dropdown Button RenderFragment</h3>
@DropdownFragment
</div>
Sample link: https://blazorplayground.syncfusion.com/BZVSNKifhJrBUecr
Conclusion
We hope you enjoyed learning about how to render the Blazor Button component using render fragment.
You can refer to our Blazor Button feature tour page to know about its other groundbreaking features. You can also explore our documentation 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 DropDownMenu 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!