c# - WPF - TreeView adding Children to Children completly dynamically -
c# - WPF - TreeView adding Children to Children completly dynamically -
i have build completly working logic in winforms insert info in treeview comes db, cant set datatable behind treeview , happy, because need build nodes , undernodes , undernodes, etc. working code in old winforms:
private void waregroup() { dataset mydataset = new dataset(); datatable mydatatable = new datatable(); string sqlstatement = "select * apwgrupp order wg_wgschl asc"; string str_key = ""; string str_desc = ""; int int_node0 = 0; int int_node1= 0; int int_node2 = 0; int int_node3 = 0; int int_node4 = 0; mydataset = importdbf(sqlstatement); mydatatable = mydataset.tables["table"]; foreach (datarow row in mydatatable.rows) { str_desc = row.field<string>(1); str_key = row.field<string>(2); switch (str_key.length) { case 1: trv_waregroup.nodes.add(str_key + "-" + str_desc); int_node0++; int_node1 = 0; int_node2 = 0; int_node3 = 0; int_node4 = 0; break; case 2: trv_waregroup.nodes[int_node0 - 1].nodes.add(str_key + "-" + str_desc); int_node1++; int_node2 = 0; int_node3 = 0; int_node4 = 0; break; case 4: trv_waregroup.nodes[int_node0 - 1].nodes[int_node1 - 1].nodes.add(str_key + "-" + str_desc); int_node2++; int_node3 = 0; int_node4 = 0; break; case 5: trv_waregroup.nodes[int_node0 - 1].nodes[int_node1 - 1].nodes[int_node2 - 1].nodes.add(str_key + "-" + str_desc); int_node3++; int_node4 = 0; break; case 6: trv_waregroup.nodes[int_node0 - 1].nodes[int_node1 - 1].nodes[int_node2 - 1].nodes[int_node3 - 1].nodes.add(str_key + "-" + str_desc); int_node4++; break; default: trv_waregroup.nodes[int_node0 - 1].nodes[int_node1 - 1].nodes[int_node2 - 1].nodes[int_node3 - 1].nodes[int_node4 - 1].nodes.add(str_key + "-" + str_desc); break; } } }
now cant utilize logic items[counter].add anymore, possible solution can think of using enumeration. found using header new item add together new node to.
i tried way, dont usable item work with:
treeviewitem temp = (treeviewitem)_waregroups.items.getitemat(node0 - 1); _waregroups.items.add(groupkey + "-" + groupdesc);
anyone got thought how can add together 2500 items way?
ok, found solution. if interested:
xaml:
<treeview itemssource="{binding items}" height="{binding elementname=adminconsolewindow, path=actualheight, converter={staticresource mathconverter}, converterparameter=(@value/3)}"> <treeview.itemtemplate> <hierarchicaldatatemplate itemssource="{binding subitems}"> <textblock text="{binding header}"/> </hierarchicaldatatemplate> </treeview.itemtemplate> </treeview>
code-behind:
public partial class adminconsole : window { public observablecollection<myitem> items { get; set; } public adminconsole() { this.items = new observablecollection<myitem>(); this.datacontext = this; initializecomponent(); } private void addwaregroups() { datatable mytable = dbase.getwaregroups(); foreach (datarow myrow in mytable.rows) { string itemdescription = myrow.field<string>(1); string itemkey = myrow.field<string>(2); myitem newitem = new myitem(); newitem.header = itemkey + " - " + itemdescription; if (itemkey.length == 1) { this.items.add(newitem); } else if (itemkey.length == 2 || itemkey.length == 5 || itemkey.length == 6) { iteratesubitems(1, newitem, itemkey, this.items); } else // if (itemkey.length == 4 || itemkey.length == 8) { iteratesubitems(2, newitem, itemkey, this.items); } } } private void iteratesubitems(int shortenlength, myitem newitem, string itemkey, observablecollection<myitem> mycollection) { foreach (myitem itemchild in mycollection) { if (itemchild.header.substring(0, itemchild.header.indexof(" - ")).equals(itemkey.substring(0, itemkey.length - shortenlength))) { itemchild.subitems.add(newitem); return; } else { if (itemchild.subitems.count > 0) { iteratesubitems(shortenlength, newitem, itemkey, itemchild.subitems); } } } }
and in class:
public class myitem : inotifypropertychanged { #region inotifypropertychanged members protected void onpropertychanged(string propertyname) { var evt = this.propertychanged; if (evt != null) evt(this, new propertychangedeventargs(propertyname)); } public event propertychangedeventhandler propertychanged; #endregion private string _header = "item"; public string header { { homecoming this._header; } set { if (this._header != value) { this._header = value; this.onpropertychanged("header"); } } } private observablecollection<myitem> _subitems = new observablecollection<myitem>(); public observablecollection<myitem> subitems { { homecoming this._subitems; } set { if (this._subitems != value) { this._subitems = value; this.onpropertychanged("subitems"); } } } }
c# wpf winforms treeview
Comments
Post a Comment