다음을 통해 공유


목록 항목의 UI 자동화 요소 찾기

참고참고

이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오.

이 항목에서는 항목의 인덱스를 알고 있을 때 목록 내 항목에서 AutomationElement를 검색하는 방법을 보여 줍니다.

예제

다음 예제에서는 목록에서 지정된 항목을 검색하는 두 가지 방법, 즉 TreeWalker를 사용하는 방법과 FindAll을 사용하는 방법을 보여 줍니다.

첫 번째 기술은 Win32 컨트롤에 대해 더 빠른 경향이 있고 두 번째는 Windows Presentation Foundation (WPF) 컨트롤에 대해 더 빠릅니다.

''' <summary>
''' Retrieves an element in a list, using TreeWalker.
''' </summary>
''' <param name="parent">The list element.</param>
''' <param name="index">The index of the element to find.</param>
''' <returns>The list item.</returns>
Function FindChildAt(ByVal parent As AutomationElement, ByVal index As Integer) As AutomationElement

    If (index < 0) Then
        Throw New ArgumentOutOfRangeException()
    End If
    Dim walker As TreeWalker = TreeWalker.ControlViewWalker
    Dim child As AutomationElement = walker.GetFirstChild(parent)
    For x As Integer = 1 To (index - 1)
        child = walker.GetNextSibling(child)
        If child = Nothing Then

            Throw New ArgumentOutOfRangeException()
        End If
    Next x
    Return child
End Function

''' <summary>
''' Retrieves an element in a list, using FindAll.
''' </summary>
''' <param name="parent">The list element.</param>
''' <param name="index">The index of the element to find.</param>
''' <returns>The list item.</returns>
Function FindChildAtB(ByVal parent As AutomationElement, ByVal index As Integer) As AutomationElement
    Dim findCondition As Condition = _
        New PropertyCondition(AutomationElement.IsControlElementProperty, True)
    Dim found As AutomationElementCollection = parent.FindAll(TreeScope.Children, findCondition)
    If (index < 0) Or (index >= found.Count) Then
        Throw New ArgumentOutOfRangeException()
    End If
    Return found(index)
End Function
/// <summary>
/// Retrieves an element in a list, using TreeWalker.
/// </summary>
/// <param name="parent">The list element.</param>
/// <param name="index">The index of the element to find.</param>
/// <returns>The list item.</returns>
AutomationElement FindChildAt(AutomationElement parent, int index)
{
    if (index < 0)
    {
        throw new ArgumentOutOfRangeException();
    }
    TreeWalker walker = TreeWalker.ControlViewWalker;
    AutomationElement child = walker.GetFirstChild(parent);
    for (int x = 1; x <= index; x++)
    {
        child = walker.GetNextSibling(child);
        if (child == null)
        {
            throw new ArgumentOutOfRangeException();
        }
    }
    return child;
}

/// <summary>
/// Retrieves an element in a list, using FindAll.
/// </summary>
/// <param name="parent">The list element.</param>
/// <param name="index">The index of the element to find.</param>
/// <returns>The list item.</returns>
AutomationElement FindChildAtB(AutomationElement parent, int index)
{
    Condition findCondition = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
    AutomationElementCollection found = parent.FindAll(TreeScope.Children, findCondition);
    if ((index < 0) || (index >= found.Count))
    {
        throw new ArgumentOutOfRangeException();
    }
    return found[index];
}

참고 항목

개념

UI 자동화 요소 가져오기