1. Run the Treeview selected event twice.
Very often, we need to execute some code inSelecteditemchanged
Depending on the selectedTreeviewitem
.Selecteditemchanged
Is called twice. This is due to stealing focus from the main window, which is screwing something up.
What we have to do to avoid this is simply delay the call to our code, I. e .,Myfunction ()
Which we need to execute inSelecteditemchanged
. Here's a workaround which delays the call to open the new window until the item selection code finishes up:
Private delegate void noargdelegate (); void windowdid selecteditemchanged (Object sender, routedpropertychangedeventargs <Object> E) {dispatcher. begininvoke (dispatcherpriority. background, (noargdelegate) delegate {myfunction ();});}
2. Obtain the parent node from Treeview
Private void treeviewincluselecteditemchanged (Object sender, routedpropertychangedeventargs <Object> E) {// node (subnode or root node) treeviewitem item = treeview1.selecteditem as treeviewitem; // obtain the parent node treeviewitem parent = item. parent as treeviewitem; // determines whether the parent node exists if (parent! = NULL) {// displays the parent node information. The header information MessageBox is displayed here. show ("parent node header:" + parent. header. tostring ();} else {MessageBox. show ("no parent node! ");}}