詳解WPF如何顯示具有層級關(guān)系的數(shù)據(jù)
前言
比方說我們有以下兩個類:
public class Class
{
public string? Name { get; set; }
public List<Student>? Students { get; set; }
}
public class Student
{
public string? Name { get; set; }
}
一個表示班級,一個表示學(xué)生,一個班級包含多個學(xué)生。在WPF中我們該如何顯示這種具有層級關(guān)系的數(shù)據(jù)呢?
今天給大家介紹的是用TreeView與HierarchicalDataTemplate進行顯示。
實現(xiàn)效果如下所示:

如果你對此感興趣,可以接著往下閱讀。
創(chuàng)建數(shù)據(jù)
private void Button_Click(object sender, RoutedEventArgs e)
{
Student student1 = new Student() { Name = "小明" };
Student student2 = new Student() { Name = "小紅" };
Student student3 = new Student() { Name = "小黃" };
Student student4 = new Student() { Name = "小綠" };
Student student5 = new Student() { Name = "小剛" };
List<Student> students1 = new List<Student>()
{
student1,
student2,
student3
};
List<Student> students2 = new List<Student>()
{
student4,
student5
};
Class class1 = new Class()
{
Name = "班級1",
Students = students1
};
Class class2 = new Class()
{
Name = "班級2",
Students = students2
};
List<Class> classes = new List<Class>()
{
class1,
class2
};
DataContext = classes;
}
數(shù)據(jù)模板的使用
xaml:
<TreeView>
<TreeViewItem ItemsSource="{Binding}" Header="全部班級"/>
</TreeView>
HierarchicalDataTemplate介紹
HierarchicalDataTemplate是WPF(Windows Presentation Foundation)中的一種數(shù)據(jù)模板,用于在樹狀結(jié)構(gòu)或?qū)哟谓Y(jié)構(gòu)中顯示數(shù)據(jù)。它允許您定義如何呈現(xiàn)包含子項的數(shù)據(jù)對象。
通過HierarchicalDataTemplate,您可以指定一個模板,用于呈現(xiàn)數(shù)據(jù)對象本身,以及一個模板,用于呈現(xiàn)其子項。這使得在TreeView等控件中輕松顯示復(fù)雜的數(shù)據(jù)結(jié)構(gòu),如文件夾和文件、組織架構(gòu)等。
通常,您會在ItemsSource屬性中指定數(shù)據(jù)源,然后使用HierarchicalDataTemplate定義每個級別的數(shù)據(jù)對象應(yīng)該如何呈現(xiàn)。
通過使用HierarchicalDataTemplate,您可以更靈活地控制數(shù)據(jù)的呈現(xiàn)方式,使您能夠創(chuàng)建具有深層次結(jié)構(gòu)的動態(tài)UI。
HierarchicalDataTemplate的使用
xaml:
<Window.Resources>
<HierarchicalDataTemplate DataType = "{x:Type local2:Class}"
ItemsSource = "{Binding Path=Students}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local2:Student}">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</Window.Resources>
我們可以發(fā)現(xiàn)對于Class類,使用了一個HierarchicalDataTemplate,ItemsSource綁定的是Class類的Students屬性。
Student類并沒有再含有層次數(shù)據(jù)了所有直接使用DataTemplate就好了。
查看實現(xiàn)效果
最后實現(xiàn)的效果如下所示:

總結(jié)
在日常開發(fā)過程中,我們可能也會有顯示層級數(shù)據(jù)的需求,本文通過一個簡單的Demo,介紹了在WPF中通過TreeView控件與HierarchicalDataTemplate層級數(shù)據(jù)模板進行層級數(shù)據(jù)的顯示。
到此這篇關(guān)于詳解WPF如何顯示具有層級關(guān)系的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)WPF顯示層級關(guān)系數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# Process.Start()系統(tǒng)找不到指定文件的問題分析及解決
文章內(nèi)容:文章描述了由于系統(tǒng)找不到可啟動的exe文件,路徑出現(xiàn)問題導(dǎo)致無法啟動應(yīng)用程序的情況,作者通過修改應(yīng)用程序的啟動路徑,將路徑中的“\\”改為“\”,從而解決了路徑問題,使應(yīng)用程序能夠正常啟動2025-11-11
C#使用泛型隊列Queue實現(xiàn)生產(chǎn)消費模式
這篇文章介紹了C#使用泛型隊列Queue實現(xiàn)生產(chǎn)消費模式的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
詳解如何通過wireshark實現(xiàn)捕獲C#上傳的圖片
這篇文章主要為大家詳細介紹了如何通過wireshark實現(xiàn)捕獲C#上傳的圖片,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下2023-11-11
C# DataSet的內(nèi)容寫成XML時如何格式化字段數(shù)據(jù)
許多讀者經(jīng)常詢問一個問題,那就是在將DataSet的內(nèi)容寫成XML時,如何格式化字段數(shù)據(jù)。最常見的需求,就是希望日期時間值與數(shù)值數(shù)據(jù)能夠以所需的格式呈現(xiàn)于XML中。2009-02-02
C#如何優(yōu)雅的對WinForm窗體應(yīng)用程序進行權(quán)限控制
經(jīng)常會出現(xiàn)winfrom頁面需要加載權(quán)限樹,下面這篇文章主要給大家介紹了關(guān)于C#如何優(yōu)雅的對WinForm窗體應(yīng)用程序進行權(quán)限控制的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11

