Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find all children controls in WPF.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.64k
    Comment on it

    I find child control using VisualTreeHelper in WPF. VisualTreeHelper provides all children in form nodes in a visual tree.

    Visual tree is used for rendering, routing and locating resource.

    Namespace of VisualTreeHelper is System.Windows.Media

     

    Here, below is the example.

     

    Adding namespaces:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

     

    Method to find all children control

    private List<Control> AllChildren(DependencyObject parent)
    		{
    			var list = new List<Control> { };
    			for (int count = 0; count < VisualTreeHelper.GetChildrenCount(parent); count++)
    			{
    				var child = VisualTreeHelper.GetChild(parent, count);
    				if (child is Control)
    				{
    					list.Add(child as Control);
    				}
    				list.AddRange(AllChildren(child));
    			}
    			return list;
    		}

     

    You have to call this method like this:

    List<Control> children=AllChildren(ItemsControl1);   

    ItemsControl1 is the name of ItemsControl. You can pass any WPF control to finds its all children control.

     

    Hope this code will help you. Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: