本文主要是介绍WPF —— TreeView树形控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 TreeView简介
TreeView 表示一个控件,该控件在树结构(其中的项可以展开和折叠)中显示分层数据。
TreeView 是一个 ItemsControl,这意味着它可以包含任何类型的对象的集合 (,例如字符串、图像或面板) 。
2 Tree View常用的属性
meun 水平菜单
contextMenu 内容菜单
TreeView 树形菜单,
SelectedItemChanged 当选择子项发生改变的时候触发
TreeViewItem 子项
3关于Tree View的实例 使用动态绑定数据
<StackPanel><TreeView Name="t1" SelectedItemChanged="t1_SelectedItemChanged"><!--静态数据绑定 TreeViewItem 子项--><TreeViewItem Header="实验小学"><TreeViewItem Header="一年级"></TreeViewItem><TreeViewItem Header="二年级"></TreeViewItem><TreeViewItem Header="三年级"></TreeViewItem><TreeViewItem Header="四年级"></TreeViewItem></TreeViewItem><TreeViewItem Header="外国语小学"><TreeViewItem Header="炼器基础篇"></TreeViewItem><TreeViewItem Header="筑基篇"></TreeViewItem><TreeViewItem Header="结丹凝丹篇"></TreeViewItem><TreeViewItem Header="元婴化婴篇"></TreeViewItem></TreeViewItem><TreeViewItem Header="裕禄小学"></TreeViewItem><!--动态绑定数据:单个对象绑定例如textblock label textbox等绑定list集合 listbox、combobox、各种菜单、datagrid德国可以绑定属性一般都是: itemsoure--><TreeView ItemsSource="{Binding }" Name="t2"><!--子项绑定是其中一个对象的属性--><TreeView.ItemTemplate><!--树形数据的模板 ItemsSource 子级菜单对象的集合--><HierarchicalDataTemplate ItemsSource="{Binding SubMenus}"><!--绑定子集标题--><TextBlock Text="{Binding Name}"></TextBlock></HierarchicalDataTemplate></TreeView.ItemTemplate></TreeView></TreeView></StackPanel>
模型类的两种方法
一:
MenuInfo m1 = new MenuInfo(){Name = "实验小学",// 存放子集标题对象SubMenus = new List<MenuInfo>(){new MenuInfo() {Name= "一年级",SubMenus= new List<MenuInfo>(){new MenuInfo() {Name = "语文"},new MenuInfo() {Name = "数学"},new MenuInfo() {Name = "体育"},new MenuInfo() {Name = "英语"},}},new MenuInfo() { Name="二年级"},new MenuInfo() { Name="三年级"},new MenuInfo() { Name="四年级"},new MenuInfo() { Name="五年级"},}};MenuInfo m2 = new MenuInfo(){Name = "特殊学校",SubMenus = new List<MenuInfo>(){new MenuInfo(){Name = "一年班"},new MenuInfo(){Name = "二年班"},}};List<MenuInfo> list = new List<MenuInfo>();list.Add(m1);list.Add(m2);this.t2.ItemsSource = list;public class MenuInfo
{public string Name { get; set; }public List<MenuInfo> SubMenus { get; set; } // 子集菜单的集合public MenuInfo(string n, MenuInfo m1) {Name = n;if(m1 != null){// 如果父级对象的subMenus 为null 则新建,如果不为空 则添加if (m1.SubMenus is null) m1.SubMenus = new List<MenuInfo>();m1.SubMenus.Add(this);}}public MenuInfo(){}
}
二:
// 标题对象MenuInfo gaibang = new MenuInfo("丐帮",null);// 二级菜单MenuInfo qiaofeng = new MenuInfo("乔峰", gaibang);MenuInfo zhuangjuxian = new MenuInfo("庄聚贤", gaibang);MenuInfo wuzhuanglao = new MenuInfo("五长老",gaibang);// 三级菜单MenuInfo xifu1 = new MenuInfo("阿紫", qiaofeng);MenuInfo xifu2 = new MenuInfo("阿紫她姐", qiaofeng);List<MenuInfo> list = new List<MenuInfo>();list.Add(gaibang);this.t2.ItemsSource = list;}private void t1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e){MessageBox.Show((t1.SelectedItem as TreeViewItem).Header.ToString());}
}
public class MenuInfo
{public string Name { get; set; }public List<MenuInfo> SubMenus { get; set; } // 子集菜单的集合public MenuInfo(string n, MenuInfo m1) {Name = n;if(m1 != null){// 如果父级对象的subMenus 为null 则新建,如果不为空 则添加if (m1.SubMenus is null) m1.SubMenus = new List<MenuInfo>();m1.SubMenus.Add(this);}}public MenuInfo(){}
}
这篇关于WPF —— TreeView树形控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!