New to Telerik UI for WPF? Download free 30-day trial

Visual Tree Helpers

The UI for WPF provides with several extension methods that allows you to enumerate the UI children of an element or find its parents. This way you can get a specific control from the visual tree interact with it.

The methods are available in the ChildrenOfTypeExtensions and ParentOfTypeExtensions static classes. They extend the DependencyObject class so you can call them on any UI element.

To use the methods you will need to include the Telerik.Windows.Controls namespace. One way to do that is to add 'using' directive that points to - using Telerik.Windows.Controls;

The extension methods would be able to enumerate the children/parents only if the visual tree of the control has been loaded. This is why you should call the methods after the Loaded event of the corresponding control was invoked.

ChildrenOfTypeExtensions

This class exposes several methods that you can use to get the children of an element.

  • ChildrenOfType<T>()—A generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the control for elements of the concrete type.

    Example 1: Getting all children of type RadToggleButton from a RadExpander control

        // Using the extension method via the DependencyObject 
        IEnumerable<RadToggleButton> toggleButtons = radExpander.ChildrenOfType<RadToggleButton>(); 
     
        // Using the method via the static class  
        IEnumerable<RadToggleButton> toggleButtons = ChildrenOfTypeExtensions.ChildrenOfType<RadToggleButton>(radExpander); 
    
  • FindChildByType<T>()—A generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the control for an element of the concrete type. The methods returns the first element it founds.

    Example 2: Getting a child type RadToggleButton from a RadExpander control

        // Using the extension method via the DependencyObject 
        RadToggleButton toggleButton = radExpander.FindChildByType<RadToggleButton>(); 
     
        // Using the method via the static class  
        RadToggleButton toggleButton = ChildrenOfTypeExtensions.FindChildByType<RadToggleButton>(radExpander); 
    

ParentOfTypeExtensions

This class exposes several methods that you can use to get the parents of an element.

  • ParentOfType<T>()—A generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the application for parent elements of the concrete type. The method returns the first parent if founds.

    Example 3: Getting the parent of type RadExpander from a RadToggleButton control

        // Using the extension method via the DependencyObject 
        RadExpander expander = radToggleButton.ParentOfType<RadExpander>(); 
     
        // Using the method via the static class  
        RadExpander expander = ParentOfTypeExtensions.ParentOfType<RadExpander>(radToggleButton); 
    
  • GetParents()—This method get all visual parents of the element. The method can be used the same way as in Example 3.

  • GetVisualParent<T>()—This method gets the first visual parent of the element. The method can be used the same way as in Example 3.

  • IsAncestorOf()—This method checks if an element is a parent of another element.

See Also

In this article