欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF中的TreeView

程序员文章站 2022-10-27 18:38:41
在wpf中实现treeview的功能,可能看到很多分享的都是简单的绑定,仅此记录自己完成的功能。 前台

在wpf中实现treeview的功能,可能看到很多分享的都是简单的绑定,仅此记录自己完成的功能。

前台

                   <treeview x:name="chaptertree"  grid.column="0"  selecteditemchanged="chaptertree_selecteditemchanged" previewmouserightbuttondown="chaptertree_previewmouserightbuttondown" fontsize="14"  itemssource="{binding path=nodes}" >
                       
                        <treeview.contextmenu>
                            <contextmenu>
                                <menuitem header="保存" name="btnalterdata" click="btnalterdata_click" />
                                <menuitem header="新增兄弟节点" name="btnsavedata" click="btnsavedata_click"/>
                                <menuitem header="新增子节点" name="btnsavesondata" click="btnsavesondata_click"/>
                                <menuitem header="删除" name="btnddeletedata" click="btnddeletedata_click"/>
                                <menuitem header="上移一位" name="btnup" click="btnup_click"/>
                                <menuitem header="下移一位" name="btndown" click="btndown_click"/>
                            </contextmenu>
                        </treeview.contextmenu>
                        <treeview.itemtemplate>
                            <hierarchicaldatatemplate datatype="{x:type c:ordermodel}" itemssource="{binding path=childnodes}">
                                <label content="{binding path=ordername}"/>
                            </hierarchicaldatatemplate>
                        </treeview.itemtemplate>
                    </treeview>

后台

        commandmodel cm;

        public command()
        {
            initializecomponent();
            cm = new commandmodel();
            chaptertree.itemssource = cm.nodes;

        }

新建了commandmodel页面用来绑定数据(从数据库提取的数据,转换成了list)

  

        public event propertychangedeventhandler propertychanged;
        private observablecollection<ordermodel> nodes = new observablecollection<ordermodel>();
        public observablecollection<ordermodel> nodes
        {
            get { return nodes; }
            set
            {
                nodes = value;
                if (propertychanged != null)
                    propertychanged(this, new propertychangedeventargs("nodes"));
            }
        }
        
        public commandmodel()
        {
            initiate();
        }
        private void initiate()
        {
            using (sqliteconnection con = new sqliteconnection(mainwindow.sb.tostring()))
            {
                datatable table = new datatable("mytable");
                var reader = con.executereader("select * from orderdata order by orderid");
                table.load(reader);
                observablecollection<ordermodel> _nodes = new observablecollection<ordermodel>();
                _nodes = converttolist(table);

                //showtreeview();
                nodes = getchildnodes(0, _nodes);

            }
        }

        private observablecollection<ordermodel> getchildnodes(long parentid, observablecollection<ordermodel> nodes)
        {
            list<ordermodel> a = new list<ordermodel>(nodes.where(x => x.parentid == parentid).tolist());
            observablecollection<ordermodel> mainnodes =new observablecollection<ordermodel>(a);
            list<ordermodel> b = new list<ordermodel>(nodes.where(x => x.parentid != parentid).tolist());
            observablecollection<ordermodel> othernodes =new observablecollection<ordermodel>(b);
            foreach (ordermodel node in mainnodes)
                node.childnodes = getchildnodes(node.id, othernodes);
            return mainnodes;
        }




        #region    datatable转list

        /// <summary>
        /// datatable转list
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static observablecollection<ordermodel> converttolist(datatable dt)
        {

            // 定义集合
            observablecollection<ordermodel> ts = new observablecollection<ordermodel>();

            // 获得此模型的类型
            type type = typeof(ordermodel);
            //定义一个临时变量
            string tempname = string.empty;
            //遍历datatable中所有的数据行
            foreach (datarow dr in dt.rows)
            {
                ordermodel t = new ordermodel();
                // 获得此模型的公共属性
                propertyinfo[] propertys = t.gettype().getproperties();
                //遍历该对象的所有属性
                foreach (propertyinfo pi in propertys)
                {
                    tempname = pi.name;//将属性名称赋值给临时变量
                    //检查datatable是否包含此列(列名==对象的属性名)  
                    if (dt.columns.contains(tempname))
                    {
                        // 判断此属性是否有setter
                        if (!pi.canwrite) continue;//该属性不可写,直接跳出
                        //取值
                        object value = dr[tempname];
                        //如果非空,则赋给对象的属性
                        if (value != dbnull.value)
                            pi.setvalue(t, value, null);
                    }
                }
                //对象添加到泛型集合中
                ts.add(t);
            }

            return ts;
        }

        #endregion

model页面的列中需要添加

        public observablecollection<ordermodel> childnodes { get; set; }
        public ordermodel()
        {
            childnodes = new observablecollection<ordermodel>();
        }