WPF Silverlight Gets child controls (gets child controls in DataTemplate)

Source: Internet
Author: User
Tags silverlight

 Public Static classvisualtreeextensions {/// <summary>        ///Get parent Node control/// </summary>        /// <typeparam name= "T" >child control UI</typeparam>        /// <param name= "obj" >child controls</param>        /// <param name= "name" >Parent container Name</param>        /// <returns>Parent Container Object</returns>         Public StaticT getparent<t> ( ThisDependencyObject obj,stringName =NULL)wheret:frameworkelement {DependencyObject parent=visualtreehelper.getparent (obj);  while(Parent! =NULL)            {                if(Parent isT && (((T) parent). Name = = Name | |string. IsNullOrEmpty (name))) {return(T) parent; } Parent=visualtreehelper.getparent (parent); }            return NULL; }        /// <summary>        ///Get child controls/// </summary>        /// <typeparam name= "T" >UI Type</typeparam>        /// <param name= "obj" >Parent Container Object</param>        /// <param name= "name" >child control Name</param>        /// <returns>child controls</returns>         Public StaticT getchild<t> ( ThisDependencyObject obj,stringName =NULL)wheret:frameworkelement {DependencyObject child=NULL; T grandchild=NULL;  for(inti =0; I <= visualtreehelper.getchildrencount (obj)-1; i++) { child=visualtreehelper.getchild (obj, i); if(Child isT && ((T) child). Name = = Name |string. IsNullOrEmpty (name))) {return(T) child; }                Else{grandchild= getchild<t>(child, name); if(Grandchild! =NULL)                        returngrandchild; }            }            return NULL; }        /// <summary>        ///Get child Controls collection/// </summary>        /// <typeparam name= "T" >UI Type</typeparam>        /// <param name= "obj" >Parent Container Object</param>        /// <param name= "name" >child control Name</param>        /// <returns>Child Controls Collection</returns>         Public StaticList<t> getchildren<t> ( ThisDependencyObject obj,stringName =NULL)wheret:frameworkelement {DependencyObject child=NULL; List<T> childlist =NewList<t>();  for(inti =0; I <= visualtreehelper.getchildrencount (obj)-1; i++) { child=visualtreehelper.getchild (obj, i); if(Child isT && ((T) child). Name = = Name | |string.                IsNullOrEmpty (name)) {Childlist.add ((T) child); } childlist.addrange (GetChildren<T> (Child,"")); }            returnchildlist; }        /// <summary>        ///find all given type of child controls collection under a control/// </summary>        /// <typeparam name= "T" >type of child control to find</typeparam>        /// <param name= "Depobj" >Parent Control</param>        /// <returns>Child Controls Collection</returns>         Public StaticT findvisualchild<t> ( ThisDependencyObject Depobj,stringChildName =NULL)wheret:frameworkelement {if(Depobj! =NULL)            {                 for(inti =0; I < Visualtreehelper.getchildrencount (depobj); i++) {DependencyObject child=Visualtreehelper.getchild (Depobj, i); if(Child! =NULL&& Child isT &&(((T) child). Name= = ChildName | |string. IsNullOrEmpty (ChildName))) {return(T) child; }                    Else{T Childofchild= findvisualchild<t>(Child, ChildName); if(Childofchild! =NULL&& Childofchild isT) {returnChildofchild; }                    }                }                return NULL; }            return NULL; }        /// <summary>        ///find all given type of child controls collection under a control/// </summary>        /// <typeparam name= "T" >type of child control to find</typeparam>        /// <param name= "Depobj" >Parent Control</param>        /// <returns>Child Controls Collection</returns>         Public StaticList<t> findvisualchildren<t> ( ThisDependencyObject Depobj,stringChildName =NULL)wheret:frameworkelement {List<T> list =NewList<t>(); if(Depobj! =NULL)            {                 for(inti =0; I < Visualtreehelper.getchildrencount (depobj); i++) {DependencyObject child=Visualtreehelper.getchild (Depobj, i); if(Child! =NULL&& Child isT&& ((T) child). Name = = ChildName | |string. IsNullOrEmpty (ChildName)) {list.                    ADD ((T) child); } List<T> ChildItems = findvisualchildren<t>(Child, ChildName); if(ChildItems! =NULL&& Childitems.count >0)                    {                        foreach(varIteminchChildItems) {list.                        ADD (item); }                    }                }            }            returnlist; }    }

WPF Silverlight Gets child controls (gets child controls in DataTemplate)

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.