|
| 1 | +--- |
| 2 | +title: Templates |
| 3 | +page_title: Treeview for Blazor | Templates |
| 4 | +description: Templates in the Treeview for Blazor |
| 5 | +slug: components/treeview/templates |
| 6 | +tags: telerik,blazor,treeview,templates |
| 7 | +published: True |
| 8 | +position: 5 |
| 9 | +--- |
| 10 | + |
| 11 | +# Treeview Templates |
| 12 | + |
| 13 | +The Treeview component allows you to define a custom template for its nodes. This article explains how you can use it. |
| 14 | + |
| 15 | +The `ItemTemplate` of a node is defined under the `TelerikTreeViewBinding` tag. |
| 16 | +@[template](/_contentTemplates/treeview/basic-example.md#data-binding-basics-link) |
| 17 | + |
| 18 | +The template receives the model to which the item is bound as its `context`. You can use it to render the desired content. |
| 19 | + |
| 20 | +You can also define different templates for the different levels in each `TelerikTreeViewBinding` tag. |
| 21 | + |
| 22 | +>caption Use templates to implement navigation between views |
| 23 | +
|
| 24 | +@[template](/_contentTemplates/treeview/basic-example.md#navigation-templates) |
| 25 | + |
| 26 | + |
| 27 | +>caption Different templates for different node levels |
| 28 | +
|
| 29 | +````CSHTML |
| 30 | +@using Telerik.Blazor.Components.TreeView |
| 31 | +
|
| 32 | +<TelerikTreeView Data="@HierarchicalData"> |
| 33 | + <TelerikTreeViewBindings> |
| 34 | + <TelerikTreeViewBinding TextField="Category" ItemsField="Products"> |
| 35 | + <ItemTemplate> |
| 36 | + Section: <strong>@((context as ProductCategoryItem).Category)</strong> |
| 37 | + </ItemTemplate> |
| 38 | + </TelerikTreeViewBinding> |
| 39 | + <TelerikTreeViewBinding Level="1" TextField="ProductName"> |
| 40 | + <ItemTemplate> |
| 41 | + @{ |
| 42 | + ProductItem currProduct = context as ProductItem; |
| 43 | + <img src="/images/products/@( currProduct.ProductId )" alt="@(currProduct.ProductName)" /> @(currProduct.ProductName) |
| 44 | + } |
| 45 | + </ItemTemplate> |
| 46 | + </TelerikTreeViewBinding> |
| 47 | + </TelerikTreeViewBindings> |
| 48 | +</TelerikTreeView> |
| 49 | +
|
| 50 | +@code { |
| 51 | + public IEnumerable<ProductCategoryItem> HierarchicalData { get; set; } |
| 52 | +
|
| 53 | + public class ProductCategoryItem |
| 54 | + { |
| 55 | + public string Category { get; set; } |
| 56 | + public List<ProductItem> Products { get; set; } |
| 57 | + public bool Expanded { get; set; } |
| 58 | + public bool HasChildren { get; set; } |
| 59 | + } |
| 60 | +
|
| 61 | + public class ProductItem |
| 62 | + { |
| 63 | + public int ProductId { get; set; } |
| 64 | + public string ProductName { get; set; } |
| 65 | + } |
| 66 | +
|
| 67 | +
|
| 68 | + protected override void OnInit() |
| 69 | + { |
| 70 | + LoadHierarchical(); |
| 71 | + } |
| 72 | +
|
| 73 | + private void LoadHierarchical() |
| 74 | + { |
| 75 | + List<ProductCategoryItem> roots = new List<ProductCategoryItem>(); |
| 76 | +
|
| 77 | + List<ProductItem> firstCategoryProducts = new List<ProductItem>() { |
| 78 | + new ProductItem { ProductName= "Product 1", ProductId = 1 }, |
| 79 | + new ProductItem { ProductName= "Product 2", ProductId = 2 } |
| 80 | + }; |
| 81 | +
|
| 82 | + roots.Add(new ProductCategoryItem |
| 83 | + { |
| 84 | + Category = "Category 1", |
| 85 | + Expanded = true, |
| 86 | + Products = firstCategoryProducts, |
| 87 | + HasChildren = firstCategoryProducts?.Count > 0, |
| 88 | +
|
| 89 | + }); |
| 90 | +
|
| 91 | + roots.Add(new ProductCategoryItem |
| 92 | + { |
| 93 | + Category = "Category 2" |
| 94 | + }); |
| 95 | +
|
| 96 | + HierarchicalData = roots; |
| 97 | + } |
| 98 | +} |
| 99 | +```` |
| 100 | + |
| 101 | + |
| 102 | +## See Also |
| 103 | + |
| 104 | + * [Data Binding a TreeView]({%slug components/treeview/data-binding/overview%}) |
| 105 | + * [Live Demo: TreeView](https://demos.telerik.com/blazor-ui/treeview/index) |
| 106 | + |
0 commit comments