• 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;
            }
        }
  • 相关阅读:
    父级和 子集 controller 之间的通讯
    ui-router(三)controller与template
    ui-router详解(二)ngRoute工具区别
    关于MySql全文索引
    Yii提供的Htmler助手checkboxList可自定义Checkbox输出格式
    添加和删除索引以及如何给中间表添加两个主键
    设置数据库及表的默认字符集
    保存数据的时候报类型错误的原因和解决方案
    金融经济
    YII获取刚插入数据的id主键
  • 原文地址:https://www.cnblogs.com/yilinyangyu/p/8073423.html
Copyright © 2020-2023  润新知