WPF Treeview background C # select the specified item and traverse it

Source: Internet
Author: User
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 ...");}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.