How to Retrieve WPF Visual Tree Programmatically ?

In previous tip, Abhishek discussed about basic of Logical and Visual Tree in WPF.  In this tip I am going to explain how you can retrieve and examine the Visual Tree programmatically in WPF. The VisualTreeHelper provides few API Methods GetChildrenCount(), GetChild() and GetParent() using which you can get details of the Visual Tree structure.

VisualTreeHelper.GetChildrenCount() method returns the number of child visuals, that the parent visual contains and VisualTreeHelper.GetChild() method helps to get the index value of the child visual object. Using the VisualTreeHelper.GetChild()  you can traverse through the visual tree of any window or child control.

To implement the same, below is the snippet for XAML where I have one Button and TreeView inside a Stack Panel and Grid is to top parent container.


On the Click of the Button we will be generating the Visual Tree. image

You must have noticed I have provided “this” as argument to the  method, which means, I want to generate the Visual Tree by considering “Main Window” as parent.

Below is the implementation for the ShowWPFVisualTree()  and AddElementsAtVisualTree() method.


You are done,  Click on “Show Visual Tree” button and you will get the complement visual Tree as show in below image.


If you want to make this bit simple, while calling ShowWPFVisualTree(), instead of passing “this” as argument, pass “grid1” as argument and run the application. You will find tree is starting from a Grid Control which contains a “Stack Panel” and Button and TreeView are the child controls.


You can Expand Button and TreeView ( Collapsed in the above image ) to get the inner tree structure.

Below is the complete Code Snippet


<Window x:Class="WPFVisualTree.MainWindow"
        Title="Show Visual Tree" Height="450" Width="536">
    <Grid x:Name="grid1">
            <Button Content="Show Visual Tree"
            <TreeView Margin="0,10,0,0" 
                      Name="treeVisual" />

Class :

public partial class MainWindow : Window
public MainWindow()


/// Handles the Click event of the btnShowVisualTree control.

/// The source of the event. /// The instance containing the event data. private void btnShowVisualTree_Click(object sender, RoutedEventArgs e)


/// Shows the WPF visual tree.

/// The element. public void ShowWPFVisualTree(DependencyObject element)
AddElementsAtVisualTree(element, null);

/// Adds the elements at visual tree.

/// The element. /// The previtem. private void AddElementsAtVisualTree(DependencyObject element, TreeViewItem previtem)
TreeViewItem item = new TreeViewItem();
item.Header = element.GetType().Name;
item.IsExpanded = true;

if (previtem == null)

int totalElementcount = VisualTreeHelper.GetChildrenCount(element);

for (int counter = 0; counter < totalElementcount; counter++) { AddElementsAtVisualTree(VisualTreeHelper.GetChild(element, counter), item); } } } Hope this will helps ! Cheers ! Aj

Subscribe To Newsletter For your Daily Development Tips and Tricks  

 Best Tips & Tricks on Microsoft Technology Platform 

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.