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)