winform treeView 数据绑定

简介: 转载:http://www.jetwu.cn/archives/737 winform treeView 数据绑定 private void Form1_Load(object sender, EventArgs e) { Types = new...

转载:http://www.jetwu.cn/archives/737

winform treeView 数据绑定

 private void Form1_Load(object sender, EventArgs e)
        {
            Types = new List<UrlTypes>()
            {
                new UrlTypes() {Id = 1, Name = "中国", Value = "0", ParentId = 0},
                new UrlTypes() {Id = 2, Name = "河南", Value = "0", ParentId = 1},
                new UrlTypes() {Id = 3, Name = "河北", Value = "0", ParentId = 1},
                new UrlTypes() {Id = 4, Name = "南阳", Value = "0", ParentId = 2},
                new UrlTypes() {Id = 4, Name = "信阳", Value = "0", ParentId = 2},
                new UrlTypes() {Id = 5, Name = "新野", Value = "0", ParentId = 4},
                new UrlTypes() {Id = 6, Name = "石家庄", Value = "0", ParentId = 3}
            };
 
            var topNode = new TreeNode();
            topNode.Name = "0";
            topNode.Text = "世界";
            treeView1.Nodes.Add(topNode);
            Bind(topNode,Types,0);
             
            treeView1.ExpandAll();
        }
      private void Bind(TreeNode parNode,List<UrlTypes> list,int nodeId )
        {
            var childList = list.FindAll(t => t.ParentId == nodeId).OrderBy(t=>t.Id);
            foreach (var urlTypese in childList)
            {
                var node = new TreeNode();
                node.Name = urlTypese.Id.ToString();
                node.Text = urlTypese.Name;
                parNode.Nodes.Add(node);
                Bind(node,list,urlTypese.Id);
            }
        }
    public class UrlTypes
    {
        public int Id { get; set; }
 
        public string Name { get; set; }
 
        public string Value { get; set; }
 
        public int ParentId { get; set; }
    }

 

目录
相关文章
|
4月前
|
C#
通过Demo学WPF—数据绑定(一)
通过Demo学WPF—数据绑定(一)
39 1
|
4月前
|
存储 开发框架 .NET
通过Demo学WPF—数据绑定(二)
通过Demo学WPF—数据绑定(二)
41 1
|
4月前
|
C#
WPF/C#:数据绑定到方法
WPF/C#:数据绑定到方法
43 0
|
数据可视化 C#
WPF技术之TreeView控件
WPF TreeView控件是一个用于显示分层数据的控件,类似于文件浏览器中的目录结构。
177 0
WinForm——TreeView总结
WinForm——TreeView总结
453 0
|
C#
Seaching TreeVIew WPF
原文:Seaching TreeVIew WPF 项目中有一个树形结构的资源,需要支持搜索功能,搜索出来的结果还是需要按照树形结构展示,下面是简单实现的demo。 1.首先创建TreeViewItem的ViewModel,一般情况下,树形结构都包含DisplayName,Deepth,Parent...
836 0
|
前端开发
MVVM里绑定TreeView控件的SelectedItem
原文:MVVM里绑定TreeView控件的SelectedItem 来源:http://stackoverflow.com/questions/9143107/get-selected-treeviewitem-using-mv...
1054 0
|
C#
WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate   ...
1668 0