![]() |
---|
このドキュメントは、System.Windows.Automation 名前空間で定義されているマネージ UI Automation クラスを使用する .NET Framework 開発者を対象としています。UI Automationに関する最新情報については、「Windows Automation API: UI Automation (Windows オートメーション API: UI オートメーション)」を参照してください。 |
ここでは、UI Automation要素からコントロール パターン オブジェクトを取得する方法について説明します。
すべてのコントロール パターンの取得
目的のコントロール パターンを持つ AutomationElement を取得します。
要素からすべてのコントロール パターンを取得するには、GetSupportedPatterns を呼び出します。
![]() |
---|
クライアントで GetSupportedPatterns を使用しないことを強くお勧めします。このメソッドは既存のコントロール パターンごとに GetCurrentPattern を内部的に呼び出すため、パフォーマンスに大きな影響が出る場合があります。可能であれば、クライアントからは対象となる主要なパターンについて GetCurrentPattern を呼び出すようにします。 |
特定のコントロール パターンの取得
目的のコントロール パターンを持つ AutomationElement を取得します。
特定のパターンを照会するには、GetCurrentPattern または TryGetCurrentPattern を呼び出します。 これらのメソッドは似ていますが、パターンが見つからない場合、GetCurrentPattern では例外が発生しますが、TryGetCurrentPattern は false を返します。
使用例
リスト項目の AutomationElement を取得し、その要素から SelectionItemPattern を取得する例を次に示します。
''' <summary>
''' Sets the focus to a list and selects a string item in that list.
''' </summary>
''' <param name="listElement">The list element.</param>
''' <param name="itemText">The text to select.</param>
''' <remarks>
''' This deselects any currently selected items. To add the item to the current selection
''' in a multiselect list, use AddToSelection instead of Select.
''' </remarks>
Public Sub SelectListItem(ByVal listElement As AutomationElement, ByVal itemText As String)
If listElement Is Nothing OrElse itemText = "" Then
Throw New ArgumentException("Argument cannot be null or empty.")
End If
listElement.SetFocus()
Dim cond As New PropertyCondition(AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase)
Dim elementItem As AutomationElement = listElement.FindFirst(TreeScope.Children, cond)
If Not (elementItem Is Nothing) Then
Dim pattern As SelectionItemPattern
Try
pattern = DirectCast(elementItem.GetCurrentPattern(SelectionItemPattern.Pattern), _
SelectionItemPattern)
Catch ex As InvalidOperationException
Console.WriteLine(ex.Message) ' Most likely "Pattern not supported."
Return
End Try
pattern.Select()
End If
End Sub 'SelectListItem
/// <summary>
/// Sets the focus to a list and selects a string item in that list.
/// </summary>
/// <param name="listElement">The list element.</param>
/// <param name="itemText">The text to select.</param>
/// <remarks>
/// This deselects any currently selected items. To add the item to the current selection
/// in a multiselect list, use AddToSelection instead of Select.
/// </remarks>
public void SelectListItem(AutomationElement listElement, String itemText)
{
if ((listElement == null) || (itemText == ""))
{
throw new ArgumentException("Argument cannot be null or empty.");
}
listElement.SetFocus();
Condition cond = new PropertyCondition(
AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase);
AutomationElement elementItem = listElement.FindFirst(TreeScope.Children, cond);
if (elementItem != null)
{
SelectionItemPattern pattern;
try
{
pattern = elementItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message); // Most likely "Pattern not supported."
return;
}
pattern.Select();
}
}