如何选择ComboBox的SelectedIndex = -1?
我编写了一个代码来自动化测试:
AutomationElement aeBuildMachine = null;
int count = 0;
do
{
Console.WriteLine("\nLooking for Build Machine Combo Box");
aeBuildMachine = aeTabitemmain.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ListBoxItem"));
if (aeBuildMachine == null)
throw new Exception("No Build Machine Combo Box");
else
Console.WriteLine("Found Build Machine Combo Box");
++count;
}
while (aeBuildMachine == null && count < 50);
Console.WriteLine("Selecting Build machine from combobox...");
SelectionItemPattern spBuildmachine = (SelectionItemPattern)aeBuildMachine.GetCurrentPattern(SelectionItemPattern.Pattern);
Run Code Online (Sandbox Code Playgroud)
我该如何使用它SelectionItemPattern?
Ste*_*son 23
这比它需要的复杂大约100倍,但我终于让它工作了.WPF ComboBox的一个大问题是,就自动化而言,在扩展ComboBox之前,它似乎没有任何ListItem .
下面的代码使用ExpandCollapse模式暂时下拉列表然后折叠它,然后它可以使用ComboBox上的FindFirst来选择ListItem,然后使用SelectionItem模式来选择它.
在原始问题的情况下,选择-1表示没有选择项目.没有这方法,但你可以简单地使用FindAll来获取ListItems的集合,依次获取每个ListItem的SelectionItem模式并调用它的RemoveFromSelection方法.
public static void SetSelectedComboBoxItem(AutomationElement comboBox, string item)
{
AutomationPattern automationPatternFromElement = GetSpecifiedPattern(comboBox, "ExpandCollapsePatternIdentifiers.Pattern");
ExpandCollapsePattern expandCollapsePattern = comboBox.GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern;
expandCollapsePattern.Expand();
expandCollapsePattern.Collapse();
AutomationElement listItem = comboBox.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, item));
automationPatternFromElement = GetSpecifiedPattern(listItem, "SelectionItemPatternIdentifiers.Pattern");
SelectionItemPattern selectionItemPattern = listItem.GetCurrentPattern(automationPatternFromElement) as SelectionItemPattern;
selectionItemPattern.Select();
}
private static AutomationPattern GetSpecifiedPattern(AutomationElement element, string patternName)
{
AutomationPattern[] supportedPattern = element.GetSupportedPatterns();
foreach (AutomationPattern pattern in supportedPattern)
{
if (pattern.ProgrammaticName == patternName)
return pattern;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
这就是我所理解的你的问题的答案。
但是..这真的是你的问题吗?
无论如何,您可以从选择中添加或删除 SelectableItems,我认为该选择属于其父项,其他一些事情也是如此,例如检查它们是否被选中。