Chinaunix首页 | 论坛 | 博客
  • 博客访问: 486118
  • 博文数量: 105
  • 博客积分: 2922
  • 博客等级: 少校
  • 技术积分: 1113
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-02 16:30
文章分类

全部博文(105)

文章存档

2018年(1)

2016年(2)

2015年(3)

2014年(6)

2013年(21)

2012年(10)

2011年(8)

2010年(7)

2009年(31)

2008年(16)

我的朋友

分类: 嵌入式

2011-12-25 23:38:46

圣诞快乐!总算弄出来了。
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\WpfTest\WpfTest\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand com = con.CreateCommand();
            com.CommandText = "select id,deptname,parentid from depttest1";
            SqlDataReader reader = com.ExecuteReader();
            List nodes = new List();
            int i=0;
            while (reader.Read())
            {
                
                Node partyGroup = new Node();
                partyGroup.ID = reader["id"].ToString();
                partyGroup.Name = reader["deptname"].ToString();
                if (reader["parentid"].ToString() != "")
                {
                    partyGroup.ParentID = reader["parentid"].ToString();
                }

                i++;
                nodes.Add(partyGroup);
            }

            List outputList = Bind(nodes);
            this.TreeView1.ItemsSource = outputList;
        }

        #region 绑定科室类
        List Bind(List nodes)
        {
            List outputList = new List();
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].ParentID == "-1") outputList.Add(nodes[i]);
                else FindDownward(nodes, nodes[i].ParentID).Nodes.Add(nodes[i]);
            }
            return outputList;
        }
        #endregion
        #region 查找父级节点
        Node FindDownward(List nodes, string id)
        {
            if (nodes == null) return null;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].ID == id)
                    return nodes[i];
                Node node = FindDownward(nodes[i].Nodes, id);
                if (node != null) return node;
            }
            return null;
        }
        #endregion
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Node node=(Node)TreeView1.SelectedItem;
            MessageBox.Show(node.ID.ToString(), node.Name);
        }

        private void TreeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            Node node = (Node)e.NewValue;
            MessageBox.Show(node.ID.ToString(), node.Name);
        }
    }
    #region 实体类
    public class Node
    {
        public Node()
        {
            this.Nodes = new List();
            this.ParentID = "-1";
        }
        public string ID { get; set; }
        public string Name { get; set; }
        public string ParentID { get; set; }
        public List Nodes { get; set; }        
    }
    #endregion

阅读(2152) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~