WPF之TreeView绑定

2021-05-19 05:30

阅读:399

标签:rtu   code   setter   lock   sources   and   img   info   com   

新建解决方案:

技术图片

StudentBll.cs代码:

 1 public class StudentBll
 2     {
 3         public List infoList { get; set; }
 4         public StudentBll()
 5         {
 6             infoList = new List();
 7             infoList.Add(new TreeItem() { Self=new StudentInfo(0, "人员信息", "", 0, -1) });
 8             infoList.Add(new TreeItem() { Self=new StudentInfo(1, "李四", "", 20, 0) });
 9             infoList.Add(new TreeItem() { Self=new StudentInfo(2, "王五", "", 26, 0) });
10             infoList.Add(new TreeItem() { Self=new StudentInfo(3, "小李", "", 23, 0) });
11             infoList.Add(new TreeItem() { Self=new StudentInfo(4, "小王", "", 20, 0) });
12             infoList.Add(new TreeItem() { Self=new StudentInfo(5, "小孙", "", 26, 0) });
13             infoList.Add(new TreeItem() { Self=new StudentInfo(6, "小刘", "", 23, 0) });
14             infoList.Add(new TreeItem() { Self=new StudentInfo(7, "小赵", "", 20, 0) });
15             infoList.Add(new TreeItem() { Self=new StudentInfo(8, "小钱", "", 26, 0) });
16         }
17         public List GetTreeViewList(int parentid, List root)
18         {
19             List rootList = root.Where(x => x.Self.ParentID == parentid).ToList();
20             List childsList = root.Where(x => x.Self.ParentID != parentid).ToList();
21             foreach (TreeItem item in rootList)
22             {
23                 item.Childs = GetTreeViewList(item.Self.ID, childsList);
24             }
25             return rootList;
26         }
27     }

StudentInfo.cs代码:

 1 public class StudentInfo
 2     {
 3         public int ID { get; set; }
 4         public string Name { get; set; }
 5         public string Sex { get; set; }
 6         public int Age { get; set; }
 7         public int ParentID { get; set; }
 8         public StudentInfo() { }
 9         public StudentInfo(int id, string name, string sex, int age, int parentid)
10         {
11             ID = id;
12             Name = name;
13             Sex = sex;
14             Age = age;
15             ParentID = parentid;
16         }
17     }

TreeItem.cs代码:

 1 public class TreeItem
 2     {
 3         public StudentInfo Self { get; set; }
 4 
 5         public List Childs { get; set; }
 6 
 7         public TreeItem()
 8         {
 9             Self = new StudentInfo();
10             Childs = new List();
11         }
12     }

StudentInfoViewModel.cs代码:

1 public class StudentInfoViewModel
2     {
3         public List Root { get; set; }   // TreeView每一项的值
4 
5         public StudentInfoViewModel()
6         {
7             Root = new List();
8         }
9     }

MainWindow.xaml代码:

 1 "TreeView绑定.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="300" Width="400" WindowStartupLocation="CenterScreen">
 5      6         "{Binding Root}">
 7              8                 
16             17             18                 "{Binding Childs}">
19                     "Horizontal">
20                         姓名:21                         "{Binding Self.Name}">22                         "10"/>
23 
24                         性别:25                         "{Binding Self.Sex}">26                         "10"/>
27 
28                         年龄:29                         "{Binding Self.Age}">30                     31                 32             33         34     35 

MainWindow.xaml.cs调用:

 1 /// 
 2     /// MainWindow.xaml 的交互逻辑
 3     /// 
 4     public partial class MainWindow : Window
 5     {
 6         public StudentBll bll { get; set; }
 7         public StudentInfoViewModel viewModel { get; set; }
 8         public MainWindow()
 9         {
10             InitializeComponent();
11             bll = new StudentBll();
12             viewModel = new StudentInfoViewModel();
13             this.DataContext = viewModel;
14             viewModel.Root = bll.GetTreeViewList(-1, bll.infoList);
15         }
16     }

 

WPF之TreeView绑定

标签:rtu   code   setter   lock   sources   and   img   info   com   

原文地址:https://www.cnblogs.com/hetong0915/p/11712251.html


评论


亲,登录后才可以留言!