WPF中TreeView绑定

xaml代码:


    
        
            
                
                    
                    
                
            
        
        
    

后台代码:

namespace WpfTest
{
    /// 
    /// TreeViewTest.xaml 的交互逻辑
    /// 
    public partial class TreeViewTest : Window
    {
        public TreeViewTest()
        {
            InitializeComponent();
            treeViewbinding();
        }
        /// 
        /// 连接数据库
        /// 
        void treeViewbinding()
        {
            DataTable dt;
            using (SqlConnection conn = new SqlConnection("server=192.168.1.152,2433;database=zhongym;uid=sa;pwd=tianlan"))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from TreeViewTest",conn);
                dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            transform(dt);
        }
        /// 
        /// 赋值给Node集合
        /// 
        /// 
        public void transform(DataTable dt)
        {
            List listNode=new List();
            foreach(DataRow dr in dt.Rows)
            {
               NodeNew temp=ConvertToTableModel(dr);
                //赋值
               listNode.Add(temp);
            }
            List outputList = Bind(listNode);
            TreeViewtest1.ItemsSource = outputList;
        }
        /// 
        /// 赋值给Node
        /// 
        /// 
        /// 
        public static NodeNew ConvertToTableModel(DataRow dr)
        {
            NodeNew result = new NodeNew();
            if (false == DBNull.Value.Equals(dr["id"].ToString())) result.ID1 = Convert.ToInt32(dr["id"]);
            if (false == DBNull.Value.Equals(dr["name"])) result.Name = dr["name"].ToString();
            if (false == DBNull.Value.Equals(dr["pid"])) result.ParentID1 = Convert.ToInt32(dr["pid"]);
            return result;
        }
        /// 
        /// 绑定树
        /// 
        List Bind(List nodes)
        {
            //定义一个返回值变量
            List outputList = new List();
            //循环遍历控件
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].ParentID1 == 0) outputList.Add(nodes[i]);//第一次调用时,ParentID为-1 也就是父级
                else FindDownward(nodes, nodes[i].ParentID1).Nodes1.Add(nodes[i]);
            }
            return outputList;
        }
        /// 
        /// 向下查找
        /// 
        /// 
        /// 
        /// 
        NodeNew FindDownward(List nodes, int id)
        {
            if (nodes == null) return null;
            for (int i = 0; i < nodes.Count; i++)
            {
                //寻找子节点
                if (nodes[i].ID1 == id)
                    return nodes[i];
                //判断NodeNew是否为空,在这里NodeNew不会为空,所以可不用
                NodeNew node = FindDownward(nodes[i].Nodes1, id);
                if (node != null) return node;
            }
            return null;
        }
    }
    /// 
    /// 节点类
    /// 
    public class NodeNew
    {
        public NodeNew()
        {
            this.Nodes1 = new List();
            this.ParentID1 = 0;
        }
        public int ID1 { get; set; }
        public string Name { get; set; }
        public int ParentID1 { get; set; }
        public List Nodes1 { get; set; }
    }
}

你可能感兴趣的:(WPF中TreeView绑定)