WPF之TreeView绑定

新建解决方案:

StudentBll.cs代码:

 1 public class StudentBll
 2     {
 3         public List<TreeItem> infoList { get; set; }
 4         public StudentBll()
 5         {
 6             infoList = new List<TreeItem>();
 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<TreeItem> GetTreeViewList(int parentid, List<TreeItem> root)
18         {
19             List<TreeItem> rootList = root.Where(x => x.Self.ParentID == parentid).ToList();
20             List<TreeItem> 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<TreeItem> Childs { get; set; }
 6
 7         public TreeItem()
 8         {
 9             Self = new StudentInfo();
10             Childs = new List<TreeItem>();
11         }
12     }

StudentInfoViewModel.cs代码:

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

MainWindow.xaml代码:

 1 <Window x:Class="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     <Grid>
 6         <TreeView ItemsSource="{Binding Root}">
 7             <TreeView.Resources>
 8                 <Style TargetType="TreeViewItem">
 9                     <Setter Property="IsExpanded" Value="True"/>
10                     <Style.Triggers>
11                         <Trigger Property="IsSelected" Value="True">
12                             <Setter Property="Foreground" Value="Red"/>
13                         </Trigger>
14                     </Style.Triggers>
15                 </Style>
16             </TreeView.Resources>
17             <TreeView.ItemTemplate>
18                 <HierarchicalDataTemplate ItemsSource="{Binding Childs}">
19                     <StackPanel Orientation="Horizontal">
20                         <TextBlock>姓名:</TextBlock>
21                         <TextBlock Text="{Binding Self.Name}"></TextBlock>
22                         <TextBlock Width="10"/>
23
24                         <TextBlock>性别:</TextBlock>
25                         <TextBlock Text="{Binding Self.Sex}"></TextBlock>
26                         <TextBlock Width="10"/>
27
28                         <TextBlock>年龄:</TextBlock>
29                         <TextBlock Text="{Binding Self.Age}"></TextBlock>
30                     </StackPanel>
31                 </HierarchicalDataTemplate>
32             </TreeView.ItemTemplate>
33         </TreeView>
34     </Grid>
35 </Window>

MainWindow.xaml.cs调用:

 1 /// <summary>
 2     /// MainWindow.xaml 的交互逻辑
 3     /// </summary>
 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     }

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

时间: 2025-01-14 18:51:09

WPF之TreeView绑定的相关文章

WPF:TreeView绑定

namespace PostViewer { using System.Collections.ObjectModel; using System.ComponentModel; /// <summary> /// 数据类型ViewModel. /// </summary> public class VmTviDataType : ITreeItem { private bool mIsExpanded = true; private bool mIsSelected; /// &

WPF TreeView绑定字典集合

1 <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width="292" > 2 3 <TreeView.ItemTemplate> 4 <HierarchicalDataTemplate ItemsSource="{Binding Value}"> 5 <StackPanel> 6

WPF TreeView绑定xaml的写法(转)

WPF TreeView绑定xaml的写法 2018年05月30日 10:16:27 dxm809 阅读数:441 方法一 <Window x:Class="TreeViewDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/x

[WPF实用技巧]如何使WPF的TreeView节点之间有连线

示例代码:TreeViewEx.zip 原文地址:http://www.codeproject.com/Tips/673071/WPF-TreeView-with-WinForms-Style-Fomat   Introduction WPF default TreeView is very good, but many people still want it to have lines join each of its child elements, like Windows Forms T

WPF元素的绑定

一.两个元素的简单绑     WPF元素的绑定,是指将两个控件绑在一起,比如利用滑动条Slider,改变刻度时,相应的标签元素中的字体的大小就会增加. 这种元素的绑定,要知道谁是目标元素,谁是源元素.比如用滑动条的刻度大小去绑定标签元素中的字体的大小.这里面滑动条的刻度就是源元素,标签字体大小就是目标元素. 在标签元素中字体大小的属性中就可以去直接绑定,绑定语句是: FontSize="{Binding ElementName=slider1,Path=Value} 解释:Binding Ele

WPF之Menu绑定XML

一.XML文件 <?xml version="1.0" encoding="utf-8" ?> <MenuData xmlns=""> <Operation Name="文件" Gesture="F"> <Operation Name="新建" Gesture="N"> <Operation Name="

TreeView绑定XmlDataSource

<?xml version="1.0" encoding="utf-8" ?> <movies> <category id="category1" text="Action"> <movie id="movie1" text="Star Wars" /> <movie id="movie2" text="

WPF MVVM TreeView 实现 右键选中 右键菜单

1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown="TreeViewItem_PreviewMouseRightButtonDown" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAli

WPF使用异步+绑定的方式处理大数据量

WPF的优势在于界面处理,即使是这样,在面对大数据量的时候也免不了界面假死,同一个线程里处理界面跟大数据量,这是不可避免的.解决办法还是有的,可以使用分页显示,虚拟加载,增加条件限制... 比较好的解决办法是使用异步+绑定的方式,即绑定控件的数据源,异步获取数据.要解决界面假死,异步获取数据是很容易想到的,但是即使这样,获取到数据之后再设置控件的数据源,这又是一个耗时的过程,所以需要绑定.如果有ViewModel(前提是实现了INotifiPropertyChanged)那就更好了,直接设置属性