Blazor的Telerik UI?下载30天免费试用

树视图数据绑定到层次数据

本文解释了如何将Blazor的TreeView绑定到层次数据。在继续之前,请确保您熟悉Treeview数据绑定基础知识

层次数据意味着集合子项是在其父模型的字段中提供的。默认情况下,这是项目字段。如果某个节点有项目,它将有一个展开图标。的HasChildren字段可以覆盖这个,但是,对于分层数据绑定来说,这不是必需的。

这种提供节点的方法允许您收集单独的数据集合和/或在每个不同的级别上使用不同的模型。请注意,数据绑定设置是按级别设置的,因此某个级别将始终使用相同的绑定,而不管它们所代表的模型及其父级是什么。

为父节点和子节点使用不同模型的分层数据示例。不需要使用不同的模型。

层次数据持有子项目的集合      @code {public IEnumerable HierarchicalData {get;设置;} public IEnumerable ExpandedItems {get;设置;} = new List();公共类ProductCategoryItem{公共字符串类别{获取;设置;} public List Products {get;设置;}}公共类ProductItem{公共字符串ProductName {get; set; } } protected override void OnInitialized() { LoadHierarchical(); ExpandedItems = HierarchicalData.Where(x => x.Products != null && x.Products.Any()).ToList(); } private void LoadHierarchical() { List roots = new List(); List firstCategoryProducts = new List() { new ProductItem { ProductName= "Category 1 - Product 1" }, new ProductItem { ProductName= "Category 1 - Product 2" } }; roots.Add(new ProductCategoryItem { Category = "Category 1", Products = firstCategoryProducts // this is how child items are provided }); roots.Add(new ProductCategoryItem { Category = "Category 2" // we will set no other properties and it will not have children, nor will it be expanded }); HierarchicalData = roots; } }
         

另请参阅

在本文中
Baidu