This documentation is archived and is not being maintained.

TreeWalker Constructor

Initializes a new instance of the TreeWalker class.

Namespace:  System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public TreeWalker(
	Condition condition


Type: System.Windows.Automation.Condition

The view of the UI Automation element tree that TreeWalker will navigate.

UI Automation elements that do not match condition are skipped over when TreeWalker is used to navigate the element tree.

If your client application might try to find elements in its own user interface, you must make all UI Automation calls on a separate thread.

The following example shows how you can construct a TreeWalker that navigates only among enabled elements.

/// <summary> 
/// Walks the UI Automation tree and adds the control type of each enabled control  
/// element it finds to a TreeView. 
/// </summary> 
/// <param name="rootElement">The root of the search on this iteration.</param>
/// <param name="treeNode">The node in the TreeView for this iteration.</param>
/// <remarks> 
/// This is a recursive function that maps out the structure of the subtree beginning at the 
/// UI Automation element passed in as rootElement on the first call. This could be, for example, 
/// an application window. 
/// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of 
/// the desktop could take a very long time and even lead to a stack overflow. 
/// </remarks> 
private void WalkEnabledElements(AutomationElement rootElement, TreeNode treeNode)
    Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
    Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
    TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
    AutomationElement elementNode = walker.GetFirstChild(rootElement);
    while (elementNode != null)
        TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);
        WalkEnabledElements(elementNode, childTreeNode);
        elementNode = walker.GetNextSibling(elementNode);

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0