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)