Wpf methods for finding parent and child elements and wpf methods for searching Elements

Source: Internet
Author: User

Wpf methods for finding parent and child elements and wpf methods for searching Elements

1 /// <summary> 2 /// search for child elements by type 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 /// <param name = "obj"> </param> 6 /// <param name = "typename"> </param> 7 /// <returns> </returns> 8 public List <T> GetChildObjects <T> (DependencyObject obj, type typename) where T: FrameworkElement 9 {10 DependencyObject child = null; 11 List <T> childList = new List <T> (); 12 13 for (int I = 0; I <= VisualTreeHelper. getChildrenCount (obj)-1; I ++) 14 {15 child = VisualTreeHelper. getChild (obj, I); 16 17 if (child is T & (T) child ). getType () = typename) 18 {19 childList. add (T) child); 20} 21 childList. addRange (GetChildObjects <T> (child, typename); 22} 23 return childList; 24}

Call 1 List <StackPanel> initToolBarWeChatUserSp = GetChildObjects <StackPanel> (name, typeof (StackPanel ));

 

1 /// <summary> 2 /// search for parent element 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5/ // <param name = "obj"> </param> 6 // <param name = "name"> </param> 7 // <returns> </returns> 8 public static T FindParent <T> (DependencyObject I _dp) where T: DependencyObject 9 {10 DependencyObject dobj = (DependencyObject) VisualTreeHelper. getParent (I _dp); 11 if (dobj! = Null) 12 {13 if (dobj is T) 14 {15 return (T) dobj; 16} 17 else18 {19 dobj = FindParent <T> (dobj ); 20 if (dobj! = Null & dobj is T) 21 {22 return (T) dobj; 23} 24} 25} 26 return null; 27}

Call 1 Border tbBorder = FindParent <Border> (name );

  

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.