AutomationElement.CachedChildren Property

Definition

Gets the cached child elements of this AutomationElement.

public System.Windows.Automation.AutomationElementCollection CachedChildren { get; }

Property Value

The collection of child elements. This collection can be empty if the element has no children.

Exceptions

The UI for the AutomationElement no longer exists.

No request was made to cache the children of this element.

Examples

In the following example, a list box element is obtained from the parent window element while a CacheRequest is active and TreeScope is Children. The specified properties of the child elements (that is, the list items) are stored in the cache and can be retrieved from the CachedChildren of the list box.

/// <summary>
/// Gets a list box element and caches the Name property of its children (the list items).
/// </summary>
/// <param name="elementMain">The UI Automation element for the parent window.</param>
void CachePropertiesWithScope(AutomationElement elementMain)
{
    AutomationElement elementList;

    // Set up the CacheRequest.
    CacheRequest cacheRequest = new CacheRequest();
    cacheRequest.Add(AutomationElement.NameProperty);
    cacheRequest.TreeScope = TreeScope.Element | TreeScope.Children;

    // Activate the CacheRequest and get the element. Note that the scope of the CacheRequest
    // is in relation to the object being retrieved: the list box and its children are 
    // cached, not the main window and its children.
    using (cacheRequest.Activate())
    {
        // Load the list element and cache the specified properties for its descendants.
        Condition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List);
        elementList = elementMain.FindFirst(TreeScope.Children, cond);
    }
    if (elementList == null) return;

    // The following illustrates that the children of the list are in the cache.
    foreach (AutomationElement listItem in elementList.CachedChildren)
    {
        Console.WriteLine(listItem.Cached.Name);
    }

    // The following call raises an exception, because the IsEnabled property was not cached.
    /*** Console.WriteLine(listItem.Cached.IsEnabled); ***/

    // The following illustrates that because the list box itself was cached, it is now
    // available as the CachedParent of each list item.
    AutomationElement child = elementList.CachedChildren[0];
    Console.WriteLine(child.CachedParent.Cached.Name);
}

Remarks

The view of the returned collection is determined by the TreeFilter condition of the CacheRequest that was active when this AutomationElement object was obtained.

Children are cached only if the scope of the CacheRequest included Subtree, Children, or Descendants.

If the CacheRequest specified that children were to be cached at this level, but there are no children, then the value of this property is 0. However, if no request was made to cache children at this level, an attempt to retrieve the property raises an exception.

Applies to

Vara Útgáfur
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also