|
| 1 | +--- |
| 2 | +title: Change the Expand and Collapse Icons in TreeView |
| 3 | +description: Learn how to customize the expand and collapse icons in the Telerik TreeView component for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize Expand and Collapse Icons in Blazor TreeView |
| 6 | +slug: treeview-kb-change-expand-collapse-icons |
| 7 | +tags: blazor, treeview, icons, expand, collapse |
| 8 | +res_type: kb |
| 9 | +ticketid: 1656109 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>TreeView for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | +I want to customize the icons used for expanding and collapsing items in the [TreeView for Blazor]({%slug treeview-overview%}). |
| 25 | + |
| 26 | +This knowledge base article answers the following questions: |
| 27 | +- How can I use custom icons for the TreeView expand and collapse functionality? |
| 28 | +- Is it possible to change the default expand and collapse icons in the TreeView for Blazor? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +You can change the expand/collapse icons in the TreeView by overriding the built-in icons with other icons using [custom CSS rules]({%slug themes-override%}). In addition, you can use the `Class` parameter of the TreeView to add a custom CSS Class and modify a specific instance of the TreeView, instead of all instances on the page. |
| 33 | + |
| 34 | +>caption Change the expand/collapse icons in TreeView |
| 35 | +
|
| 36 | +````CSHTML |
| 37 | +@* In Telerik.UI.for.Blazor version 4.3.0 and later, the components use SVG icons by default. Use the following CSS for versions 4.3.0 and later. *@ |
| 38 | +@* Render the desired SVG icon and inspect it with your dev tools to get its path. *@ |
| 39 | +<style> |
| 40 | + .custom-icons .k-treeview-toggle .k-svg-icon.k-svg-i-caret-alt-down svg path { |
| 41 | + d: path("m382.059 158.059-126.06 126.06-126.061-126.06L96 192l159.999 160L416 192z"); |
| 42 | + } |
| 43 | +
|
| 44 | + .custom-icons .k-treeview-toggle .k-svg-icon.k-svg-i-caret-alt-right svg path { |
| 45 | + d: path("m158.059 129.941 126.06 126.06-126.06 126.061L192 416l160-159.999L192 96z"); |
| 46 | +</style> |
| 47 | +
|
| 48 | +@* In Telerik.UI.for.Blazor version below 4.3.0, the components use Font icons by default. Use this CSS for versions prior to 4.3.0. *@ |
| 49 | +@* Copy the unicode of your desired icon from the Progress Design System - https://www.telerik.com/design-system/docs/foundation/iconography/icon-list/ *@ |
| 50 | +
|
| 51 | +@* <style> |
| 52 | + .custom-icons .k-treeview-toggle .k-icon.k-i-caret-alt-down::before { |
| 53 | + content: "\e015"; |
| 54 | + } |
| 55 | +
|
| 56 | + .custom-icons .k-treeview-toggle .k-icon.k-i-caret-alt-right::before { |
| 57 | + content: "\e014"; |
| 58 | + } |
| 59 | +</style> *@ |
| 60 | +
|
| 61 | +<TelerikTreeView Class="custom-icons" |
| 62 | + Data="@FlatData" |
| 63 | + @bind-ExpandedItems="@ExpandedItems" /> |
| 64 | +
|
| 65 | +@code { |
| 66 | + private IEnumerable<TreeItem> FlatData { get; set; } |
| 67 | +
|
| 68 | + private IEnumerable<object> ExpandedItems { get; set; } = new List<TreeItem>(); |
| 69 | +
|
| 70 | + protected override void OnInitialized() |
| 71 | + { |
| 72 | + FlatData = GetFlatData(); |
| 73 | +
|
| 74 | + ExpandedItems = FlatData.Where(x => x.HasChildren == true && x.Id != 3).ToList(); |
| 75 | + } |
| 76 | +
|
| 77 | + List<TreeItem> GetFlatData() |
| 78 | + { |
| 79 | + List<TreeItem> items = new List<TreeItem>(); |
| 80 | +
|
| 81 | + items.Add(new TreeItem() |
| 82 | + { |
| 83 | + Id = 1, |
| 84 | + Text = "wwwroot", |
| 85 | + ParentId = null, |
| 86 | + HasChildren = true, |
| 87 | + Icon = SvgIcon.Folder |
| 88 | + }); |
| 89 | + items.Add(new TreeItem() |
| 90 | + { |
| 91 | + Id = 2, |
| 92 | + Text = "css", |
| 93 | + ParentId = 1, |
| 94 | + HasChildren = true, |
| 95 | + Icon = SvgIcon.Folder |
| 96 | + }); |
| 97 | + items.Add(new TreeItem() |
| 98 | + { |
| 99 | + Id = 3, |
| 100 | + Text = "js", |
| 101 | + ParentId = 1, |
| 102 | + HasChildren = true, |
| 103 | + Icon = SvgIcon.Folder |
| 104 | + }); |
| 105 | + items.Add(new TreeItem() |
| 106 | + { |
| 107 | + Id = 4, |
| 108 | + Text = "site.css", |
| 109 | + ParentId = 2, |
| 110 | + Icon = SvgIcon.Css |
| 111 | + }); |
| 112 | + items.Add(new TreeItem() |
| 113 | + { |
| 114 | + Id = 5, |
| 115 | + Text = "scripts.js", |
| 116 | + ParentId = 3, |
| 117 | + Icon = SvgIcon.Js |
| 118 | + }); |
| 119 | +
|
| 120 | + return items; |
| 121 | + } |
| 122 | +
|
| 123 | + public class TreeItem |
| 124 | + { |
| 125 | + public int Id { get; set; } |
| 126 | + public string Text { get; set; } |
| 127 | + public int? ParentId { get; set; } |
| 128 | + public bool HasChildren { get; set; } |
| 129 | + public ISvgIcon Icon { get; set; } |
| 130 | + } |
| 131 | +} |
| 132 | +```` |
| 133 | +## See Also |
| 134 | +- [TreeView Overview]({%slug treeview-overview%}) |
| 135 | +- [Telerik Icons List](https://www.telerik.com/design-system/docs/foundation/iconography/icon-list/) |
0 commit comments