Modifica

Condividi tramite


TreeWalker.GetParent Method

Definition

Retrieves the parent element of the specified AutomationElement.

Overloads

GetParent(AutomationElement)

Retrieves the parent element of the specified AutomationElement.

GetParent(AutomationElement, CacheRequest)

Retrieves the parent element of the specified AutomationElement and caches properties and patterns.

Remarks

The structure of the AutomationElement tree changes as the visible user interface (UI) elements on the desktop change. It is not guaranteed that an element returned as the parent element will be returned as the parent on subsequent passes.

GetParent(AutomationElement)

Retrieves the parent element of the specified AutomationElement.

public:
 System::Windows::Automation::AutomationElement ^ GetParent(System::Windows::Automation::AutomationElement ^ element);
public System.Windows.Automation.AutomationElement GetParent (System.Windows.Automation.AutomationElement element);
member this.GetParent : System.Windows.Automation.AutomationElement -> System.Windows.Automation.AutomationElement
Public Function GetParent (element As AutomationElement) As AutomationElement

Parameters

element
AutomationElement

The element whose parent is to be returned.

Returns

The parent element, or a null reference (Nothing in Visual Basic) if the specified element is the root element in the tree, or if the parent element is not visible in the current view.

Examples

The following example shows GetParent being used to find the window element that contains a specified element.

/// <summary>
/// Retrieves the top-level window that contains the specified UI Automation element.
/// </summary>
/// <param name="element">The contained element.</param>
/// <returns>The containing top-level window element.</returns>
private AutomationElement GetTopLevelWindow(AutomationElement element)
{
    TreeWalker walker = TreeWalker.ControlViewWalker;
    AutomationElement elementParent;
    AutomationElement node = element;
    if (node == elementRoot) return node;
    do
    {
        elementParent = walker.GetParent(node);
        if (elementParent == AutomationElement.RootElement) break;
        node = elementParent;
    }
    while (true);
    return node;
}
    ''' <summary>
    ''' Retrieves the top-level window that contains the specified UI Automation element.
    ''' </summary>
    ''' <param name="element">The contained element.</param>
    ''' <returns>The containing top-level window element.</returns>
    Private Function GetTopLevelWindow(ByVal element As AutomationElement) As AutomationElement 
        Dim walker As TreeWalker = TreeWalker.ControlViewWalker
        Dim elementParent As AutomationElement
        Dim node As AutomationElement = element
        If node = elementRoot Then
            Return node
        End If
        Do
            elementParent = walker.GetParent(node)
            If elementParent = AutomationElement.RootElement Then
                Exit Do
            End If
            node = elementParent
        Loop While True
        Return node
    
    End Function 'GetTopLevelWindow
End Class

See also

Applies to

GetParent(AutomationElement, CacheRequest)

Retrieves the parent element of the specified AutomationElement and caches properties and patterns.

public:
 System::Windows::Automation::AutomationElement ^ GetParent(System::Windows::Automation::AutomationElement ^ element, System::Windows::Automation::CacheRequest ^ request);
public System.Windows.Automation.AutomationElement GetParent (System.Windows.Automation.AutomationElement element, System.Windows.Automation.CacheRequest request);
member this.GetParent : System.Windows.Automation.AutomationElement * System.Windows.Automation.CacheRequest -> System.Windows.Automation.AutomationElement
Public Function GetParent (element As AutomationElement, request As CacheRequest) As AutomationElement

Parameters

element
AutomationElement

The element whose parent is to be returned.

request
CacheRequest

A cache request object specifying members on the returned AutomationElement to cache.

Returns

The parent element, or a null reference (Nothing in Visual Basic) if the specified element is the root element in the tree, or the parent element is not visible in this view.

Remarks

The structure of the AutomationElement tree changes as the visible user interface (UI) elements on the desktop change. It is not guaranteed that an element returned as the parent element will be returned as the parent on subsequent passes.

See also

Applies to