c# - Adding a child node in the middle of a list of other children -
this question follow this question. want add nodes treeview in numerical order. right working list of child nodes. each child has displayname of numerical value. in program have loop checks if node being added has displayname less displayname of current child in loop. if new child's displayname less child being checked add new node before existing one. having trouble figuring out method this.
here code:
var node = data.getallchildren(x => x.children).distinct().tolist().first(x => x.identify == 'b'); //get # of children -- if children exist if (node.children.count() > 0) { newchildtitle = int.parse(nodename.value); //node add (int = 0; < node.children.count(); i++) { currentchildtitle = int.parse(node.children.elementat(i).displayname.value); //current node value if (newchildtitle == currentchildtitle) //check if location exists { messagebox.show("sorry, location name exists under category.", "duplicate location name", messageboxbutton.ok, messageboximage.warning); break; } else if (newchildtitle < currentchildtitle) //if new location value less, add before current location in tree { //code go here** break; } } } else //if no children exist, add node.children.add(createblockinglocation(model, blocktreecollection, nodename)); xaml of treeview (the treeview bound observablecollection):
<!-- tree view items & functions --> <treeview name="tree_one" isenabled="{binding datacontext.datatreeenabled, relativesource={relativesource findancestor, ancestortype={x:type window}}}" itemssource="{binding datatree.data}" /> data , properties of observablecollection<hierarchicalvm> treeview bound to:
private hierarchicalvm _parent; private observablecollection<hierarchicalvm> _children; public hierarchicalvm() { commands = new observablecollection<command>(); } //relationship properties public hierarchicalvm parent { { return _parent; } set { _parent = value; notifypropertychange(() => parent); } } public observablecollection<hierarchicalvm> children { { return _children ?? (_children = new observablecollection<hierarchicalvm>()); } set { _children = value; } } current solution attempt:
//gets parent item (data complete tree) var node = data.getallchildren(x => x.children).distinct().tolist().first(x => x.identify == 'b'); if (!node.children.any(n => n.numvalue == newnumvalue)) { //error on line: **operator '.' cannot applied operand of type 'void' node.children = node.children.add(newnode).orderby(n => n.numvalue); }
ok, assuming have reference parent object (not datatreeitem ui element), there 2 ways can think of accomplish this. off top of head little bit of checking against msdn, apologize bugs.
parent parent = //however getting parent if (!parent.children.any(n => n.displayname == newnode.displayname)) { parent.children.add(newnode) parent.children = new observablecollection<viewmodel>(parent.children.orderby(n => n.displayname)); //or, parent.children.insertitem(parent.children.indexof(parent.children.orderby(n => n.displayname).last(n => n.displayname < newnode.displayname)), newnode); } the first method adds new item , resorts list, gets same result in-place insert. second in-place insert, more complicated because have find index insert at.
if list sorted remove orderby in second method. assumes displayname int , property of each child node (which code posted isn't, should be).
clearly don't understand architecture, explain how think should be, , perhaps can discover code broken.
we have object type called parent, contains collection called children, so:
class parent<t> { observablecollection<t> children; } now have collection of parent called data in view model treeview bound to. somehow, getting single instance of parent class (which shouldn't void, fact "node.children" object should huge red flag).
if agree said above, insert code posted should work.
please let me know if can clarify or add answer, or in other way.
Comments
Post a Comment