|
| 1 | +--- |
| 2 | +title: Hierarchical Breadcrumb in Blazor |
| 3 | +description: Learn how to customize the Breadcrumb for Blazor by embedding dropdowns within Breadcrumb items to optimize user interaction and functionality. |
| 4 | +type: how-to |
| 5 | +page_title: Hierarchical Breadcrumb in Blazor |
| 6 | +slug: hierarchical-breadcrumb-dropdowns-blazor |
| 7 | +tags: breadcrumb, blazor, customization, dropdown, itemtemplate, hierarchical |
| 8 | +res_type: kb |
| 9 | +ticketid: 1652751 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Breadcrumb for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +Adding custom components like dropdowns to Breadcrumb crumbs can improve user interaction. |
| 26 | + |
| 27 | +This KB article answers the following questions: |
| 28 | +* Is it possible to achieve a hierarchical structure in the Breadcrumb component? |
| 29 | +* Is it possible to embed custom components inside Breadcrumb crumbs? |
| 30 | +* How to use the `ItemTemplate` to add dropdowns to Breadcrumb items? |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To embed dropdowns in the Breadcrumb "crumbs", use an [`ItemTemplate`]({%slug breadcrumb-templates%}#itemtemplate). This template allows you to customize the Breadcrumb items, and include other components such as dropdowns. |
| 35 | + |
| 36 | +````CSHTML |
| 37 | +@*The dropdown's appearance is customized to blend with the Breadcrumb by adjusting the border color and preventing text decoration changes on hover.*@ |
| 38 | +
|
| 39 | +<TelerikBreadcrumb Data="@Items"> |
| 40 | + <ItemTemplate> |
| 41 | + @if (context.Items != null && context.Items.Any()) |
| 42 | + { |
| 43 | + var values = new List<string> { context.Text }.Concat(context.Items.Select(x => x.Text)); |
| 44 | + var value = context.SelectedChildren?.Text ?? context.Text; |
| 45 | + <TelerikDropDownList FillMode="@ThemeConstants.DropDownList.FillMode.Flat" |
| 46 | + Data="@values" |
| 47 | + Class="breadcrumb-ddl" |
| 48 | + Value="@value" |
| 49 | + ValueChanged="@((string value) => OnValueChanged(context, value))"> |
| 50 | + <DropDownListSettings> |
| 51 | + <DropDownListPopupSettings Height="auto" /> |
| 52 | + </DropDownListSettings> |
| 53 | + </TelerikDropDownList> |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + <span>@context.Text</span> |
| 58 | + } |
| 59 | + </ItemTemplate> |
| 60 | +</TelerikBreadcrumb> |
| 61 | +
|
| 62 | +<style> |
| 63 | + .breadcrumb-ddl { |
| 64 | + border-color: transparent !important; |
| 65 | + } |
| 66 | +
|
| 67 | + .k-breadcrumb-link:hover:has(.k-dropdownlist) { |
| 68 | + text-decoration: none; |
| 69 | + } |
| 70 | +</style> |
| 71 | +
|
| 72 | +@code { |
| 73 | + private IEnumerable<BreadcrumbItem> Items { get; set; } = new List<BreadcrumbItem>(); |
| 74 | +
|
| 75 | + private void OnValueChanged(BreadcrumbItem item, string value) |
| 76 | + { |
| 77 | + item.SelectedChildren = item.Items.FirstOrDefault(x => x.Text == value); |
| 78 | + } |
| 79 | +
|
| 80 | + protected override void OnInitialized() |
| 81 | + { |
| 82 | + Items = new List<BreadcrumbItem> |
| 83 | + { |
| 84 | + new BreadcrumbItem { Text = "Telerik UI for Blazor" }, |
| 85 | + new BreadcrumbItem { Text = "Components", Items = new List<BreadcrumbItem> { new BreadcrumbItem { Text = "AutoComplete" }, new BreadcrumbItem { Text = "Calendar" }, new BreadcrumbItem { Text = "Grid" } } }, |
| 86 | + new BreadcrumbItem { Text = "Templates" }, |
| 87 | + }; |
| 88 | + } |
| 89 | +
|
| 90 | + public class BreadcrumbItem |
| 91 | + { |
| 92 | + public string Text { get; set; } |
| 93 | + public List<BreadcrumbItem> Items { get; set; } |
| 94 | + public BreadcrumbItem SelectedChildren { get; set; } |
| 95 | + } |
| 96 | +} |
| 97 | +```` |
| 98 | + |
| 99 | +## See Also |
| 100 | + |
| 101 | +* [Breadcrumb Templates](https://docs.telerik.com/blazor-ui/components/breadcrumb/templates#itemtemplate) |
| 102 | +* [DropDownList Overview](https://docs.telerik.com/blazor-ui/components/dropdownlist/overview) |
0 commit comments