GetParent Method (AutomationElement)
Collapse the table of content
Expand the table of content

TreeWalker.GetParent Method (AutomationElement)


Retrieves the parent element of the specified AutomationElement.

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

Public Function GetParent (
	element As AutomationElement
) As AutomationElement


Type: System.Windows.Automation.AutomationElement

The element whose parent is to be returned.

Return Value

Type: System.Windows.Automation.AutomationElement

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

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 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
            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 'Reader 

.NET Framework
Available since 3.0
Return to top
© 2016 Microsoft