首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

WPF TreeGrid(树形铺展的表格)

2012-10-14 
WPF TreeGrid(树形展开的表格)一直都想要个树形展开的表 像这样的今天心血来潮就简单做了个TreeGrid 喜欢

WPF TreeGrid(树形展开的表格)

一直都想要个树形展开的表格 像这样的

WPF TreeGrid(树形铺展的表格)


今天心血来潮就简单做了个TreeGrid 喜欢的同学可以下载下去自己研究下,其实也比较简单主要就是TreeView TreeViewItem再配合GridViewHeaderRowPresenter、GridViewRowPresenter、GridViewColumnCollection定制style基本上就可以实现以上效果

本文中涉及大量模板,绑定等知识,如果刚刚入门还没有了解以上知识的同学可以先补下课,然后再来看,以免浪费时间。

闲话不多说,直接上代码:

前台代码 MainWindow.xaml

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.ComponentModel;using System.Collections.ObjectModel;namespace TreeGrid{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            ObjForTest root = new ObjForTest("root", "root",0,"",0);            ObjForTest c1 = new ObjForTest("CEO", "Leo",45,"M",1);            ObjForTest c2 = new ObjForTest("CFO", "Tami",46,"FM",1);            ObjForTest c3 = new ObjForTest("COO", "Jack",47,"M",1);            ObjForTest cc1 = new ObjForTest("Manager", "John", 30, "M", 2);            ObjForTest cc2 = new ObjForTest("Manager", "Lee", 31, "FM", 2);            ObjForTest cc3 = new ObjForTest("Manager", "Chris", 32, "M", 2);            ObjForTest ccc1 = new ObjForTest("Worker", "Evan", 25,"FM",3);            root.Children.Add(c1);            root.Children.Add(c2);            root.Children.Add(c3);            c1.Children.Add(cc1);            c2.Children.Add(cc2);            c3.Children.Add(cc3);            cc1.Children.Add(ccc1);            this._list.ItemsSource = root.Children;        }    }    public class ObjForTest : INotifyPropertyChanged    {        public ObjForTest(string title, string name,int age,string sex,int level)        {            this._jobTitle = title;            this._sex = sex;            this._age = age;            this._name = name;            this._level = level;        }        private string _name;        private int _age;        private string _sex;        private int _level;        private string _jobTitle;        public string Sex { get { return this._sex; } set { this._sex = value; } }        public int Age { get { return this._age; } set { this._age = value; } }        public int Level        {            get             {                 return this._level;            }            set            {                _level = value;                if (PropertyChanged != null)                    PropertyChanged(this, new PropertyChangedEventArgs("Level"));            }        }        public string JobTitle        {            get { return _jobTitle; }            set            {                _jobTitle = value;                if (PropertyChanged != null)                    PropertyChanged(this, new PropertyChangedEventArgs("JobTitle"));            }        }        public string Name        {            get { return _name; }            set            {                _name = value;                if (PropertyChanged != null)                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));            }        }        private ObservableCollection<ObjForTest> _children = new ObservableCollection<ObjForTest>();        public ObservableCollection<ObjForTest> Children        {            get { return _children; }        }        public event PropertyChangedEventHandler PropertyChanged;    }}

ObjForTest是用来绑定的对象this._list.ItemsSource = root.Children;这句是用来绑定对象的children对象

完整实例可以  点击此处下载   如果有更好的方法也可以给我留言 谢谢!

热点排行