wpf treeview 数据绑定 递归绑定节点
Posted 挖煤小弟的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf treeview 数据绑定 递归绑定节点相关的知识,希望对你有一定的参考价值。
1.先上效果
将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点。
1.xaml文件,将以下代码加入界面合适位置
1 <StackPanel> 2 <StackPanel Margin="10"> 3 <Label Content="选择组节点:"></Label> 4 <ComboBox MaxDropDownHeight="100" Name="cmbGoup" DropDownClosed="cmbGoup_DropDownClosed"></ComboBox> 5 </StackPanel> 6 <StackPanel Margin ="10"> 7 <TreeView x:Name="tvGroup"> 8 <TreeView.ItemTemplate> 9 <HierarchicalDataTemplate ItemsSource="{Binding Nodes}"> 10 <StackPanel> 11 <TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding GroupName}" Margin="2,0,0,0"></TextBlock> 12 </StackPanel> 13 </HierarchicalDataTemplate> 14 </TreeView.ItemTemplate> 15 </TreeView> 16 </StackPanel> 17 </StackPanel>
2.后台代码
a.用于绑定的节点类
1 public class Group 2 { 3 public Group() 4 { 5 this.Nodes = new List<Group>(); 6 this.ParentId = 0;//主节点的父id默认为0 7 } 8 9 public List<Group> Nodes { get; set; } 10 public int ID { get; set; }//id 11 public int ParentId { get; set; }//parentID 12 public string GroupName { get; set; } 13 }
b.主界面类代码
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); #region 用于绑定的数据 List<Group> grpLst = new List<Group>(){ new Group(){ID=0,GroupName="Group", ParentId = -1}, new Group(){ID=1,GroupName="Group1",ParentId=0}, new Group(){ID=2,GroupName="Group2",ParentId=0}, new Group(){ID=3,GroupName="Group1_1",ParentId=1}, new Group(){ID=4,GroupName="Group1_2",ParentId=1}, new Group(){ID=5,GroupName="Group1_3",ParentId=1}, new Group(){ID=6,GroupName="Group1_4",ParentId=1}, new Group(){ID=7,GroupName="Group1_5",ParentId=1}, new Group(){ID=8,GroupName="Group2_1",ParentId=2}, new Group(){ID=9,GroupName="Group2_2",ParentId=2}, new Group(){ID=10,GroupName="Group2_3",ParentId=2}, new Group(){ID=11,GroupName="Group2_4",ParentId=2}, new Group(){ID=12,GroupName="Group1_1_1",ParentId=3}, new Group(){ID=13,GroupName="Group1_1_2",ParentId=3}, new Group(){ID=14,GroupName="Group1_2_1",ParentId=4}, new Group(){ID=15,GroupName="Group1_1_1_1",ParentId=12} }; #endregion this.cmbGoup.ItemsSource = grpLst;//comboBox数据源 this.cmbGoup.SelectedValuePath = "ID"; this.cmbGoup.DisplayMemberPath = "GroupName"; List<Group> lstGroup = getTreeData(-1, grpLst);//初始化时获取父节点为-1的数据 this.tvGroup.ItemsSource = lstGroup;//数据绑定 } /// <summary> /// 递归生成树形数据 /// </summary> /// <param name="delst"></param> /// <returns></returns> public List<Group> getTreeData(int parentid, List<Group> nodes) { List<Group> mainNodes = nodes.Where(x => x.ParentId == parentid).ToList<Group>(); List<Group> otherNodes = nodes.Where(x => x.ParentId != parentid).ToList<Group>(); foreach (Group grp in mainNodes) { grp.Nodes = getTreeData(grp.ID, otherNodes); } return mainNodes; } /// <summary> /// 下拉框关闭事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmbGoup_DropDownClosed(object sender, EventArgs e) { if (this.cmbGoup.SelectedValue == null) { return; } int groupId = (int)this.cmbGoup.SelectedValue;//选中的组号 List<Group> lstGroup = getTreeData(groupId, (List<Group>)cmbGoup.ItemsSource); this.tvGroup.ItemsSource = lstGroup; } }
以上是关于wpf treeview 数据绑定 递归绑定节点的主要内容,如果未能解决你的问题,请参考以下文章
Windows 窗体 TreeView - 绑定分层递归数据源