wpf之TreeView
<Window x:Class="MyWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyWpf" mc:Ignorable="d" Title="MainWindow" Height="296.042" Width="347.333"> <Grid> <TreeView HorizontalAlignment="Left" Height="247" Margin="10,10,0,0" VerticalAlignment="Top" Width="135"> <!--IsExpanded=true表示展开节点 --> <TreeViewItem Header="学生管理系统" IsExpanded="True"> <!-- IsSelected是否被选中--> <TreeViewItem Header="系统管理系统" IsExpanded="True" IsSelected="True"> <TreeViewItem Header="角色管理系统" IsExpanded="True"> </TreeViewItem> </TreeViewItem> </TreeViewItem> </TreeView> </Grid> </Window>
上面是静态简单生成一个树形结构
结果:
下面是一个非常简单的动态加载的树形:
<Window x:Class="MyWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyWpf" mc:Ignorable="d" Title="MainWindow" Height="296.042" Width="347.333" Loaded="Window_Loaded"> <Grid> <TreeView x:Name="tvList" HorizontalAlignment="Left" MinHeight="247" Height="auto" Margin="10,10,0,0" VerticalAlignment="Top" Width="90"> <!--IsExpanded=true表示展开节点 --> </TreeView> </Grid> </Window>
后台代码:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { //代码添加 TreeViewItem ti = new TreeViewItem(); ti.Header = "基础信息管理系统"; //设置信息悬浮提示 ti.ToolTip = ti.Header; tvList.Items.Add(ti); TreeViewItem ti1 = new TreeViewItem(); ti1.Header = "用户管理系统"; ti1.ToolTip = ti1.Header; ti.Items.Add(ti1); TreeViewItem ti2 = new TreeViewItem(); ti2.Header = "角色管理系统"; ti2.ToolTip = ti2.Header; ti.Items.Add(ti2); } }
结果:
下面是项目开发中通常用的,模型是自定义的:
<Window x:Class="MyWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyWpf" xmlns:model="clr-namespace:MyWpf" mc:Ignorable="d" Title="MainWindow" Height="296.042" Width="347.333" Loaded="Window_Loaded"> <Grid> <TreeView x:Name="tvList" HorizontalAlignment="Left" MinHeight="247" Height="auto" Margin="10,10,0,0" VerticalAlignment="Top" Width="90" ItemsSource="{Binding TreeList}" ToolTip="sd" > <TreeView.ItemTemplate> <!--DataType中使用情况的前提是引入了TreeViewTestModel模型,在这里是在上面通过 xmlns:model="clr-namespace:MyWpf引入的" --> <HierarchicalDataTemplate DataType="{x:Type model:TreeViewTestModel}" ItemsSource="{Binding Items}"> <!-- ToolTip加悬浮提示--> <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}"> </TextBlock> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Grid> </Window>
后台:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MyWpf { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { List<TreeViewTestModel> list = new List<TreeViewTestModel>(); TreeViewTestModel tree = new TreeViewTestModel(); tree.Id = "1"; tree.Name = "系统01"; tree.ToolTip = tree.Name; TreeViewTestModel tree2 = new TreeViewTestModel(); tree2.Id = "2"; tree2.Name = "系统02"; tree2.ToolTip = tree2.Name; tree.Items.Add(tree2); TreeViewTestModel tree3 = new TreeViewTestModel(); tree3.Id = "3"; tree3.Name = "系统03"; tree3.ToolTip = tree3.Name; tree.Items.Add(tree3); list.Add(tree); TLModel tL = new TLModel(); tL.TreeList = list; this.DataContext = tL; } } public class TLModel { public List<TreeViewTestModel> TreeList { get; set; } } public class TreeViewTestModel { public TreeViewTestModel() { Items = new List<TreeViewTestModel>(); } public string Id { get; set; } public string ToolTip { get; set; } public string Name { get; set; } public List<TreeViewTestModel> Items { get; set; } } }
WPF使用模板HierarchicalDataTemplate实现TreeView层次显示
从
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术