Private treeviewitem findtreeviewitem (itemscontrol container, object item) {If (null = container | null = item) {return NULL ;}
If (container. datacontext = item) {return container as treeviewitem ;}
Int COUNT = container. Items. Count; For (INT I = 0; I <count; I ++) {treeviewitem subcontainer = (treeviewitem) container. itemcontainergenerator. containerfromindex (I );
If (null = subcontainer) {continue ;}
// Search the next level for the object. treeviewitem resultcontainer iner = findtreeviewitem (subcontainer, item); If (null! = Resultcontainer) {return resultcontainer ;}}
Return NULL ;}
//// Use the preceding method to traverse and set the isselected attribute
Private Static void selecteditemchanged (dependencyobject D, dependencypropertychangedeventargs e) {// selectedprotocolcontrol SPC = D as selectedprotocolcontrol;
If (null! = SPC) {If (E. newvalue! = SPC. trvselectedprotocol. selecteditem) {// treeviewitem tvinew = SPC. trvselectedprotocol. itemcontainergenerator. containerfromitem (E. newvalue) as treeviewitem; treeviewitem tvinew = SPC. findtreeviewitem (SPC. trvselectedprotocol, E. newvalue); If (null! = Tvinew) {tvinew. isselected = true ;}}
If (null! = E. oldvalue) {// treeviewitem tviold = SPC. trvselectedprotocol. itemcontainergenerator. containerfromitem (E. oldvalue) as treeviewitem; treeviewitem tviold = SPC. findtreeviewitem (SPC. trvselectedprotocol, E. oldvalue); If (null! = Tviold) {tviold. isselected = false ;}}}
System. Diagnostics. Debug. writeline ("selecteditemchanged ...");}