Rechercher un élément UI Automation pour un élément de liste
Notes
Cette documentation s’adresse aux développeurs .NET Framework qui souhaitent utiliser les classes UI Automation managées définies dans l’espace de noms System.Windows.Automation. Pour obtenir les dernières informations sur UI Automation, consultez API Windows Automation : UI Automation.
Cette rubrique montre comment récupérer un élément AutomationElement dans une liste lorsque l’index de l’élément est connu.
Exemple
L’exemple suivant montre deux façons de récupérer un élément spécifié à partir d’une liste, l’une à l’aide de TreeWalker et l’autre à l’aide de FindAll.
La première technique tend à être plus rapide pour les contrôles Win32, mais la seconde est plus rapide pour les contrôles 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>
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];
}
''' <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