[英]Select custom ComboBox item with UI Automation
如何使用Microsoft UI Automation在自定义ComboBox中选择项目? 我有这样的ComboBox:
<ComboBox AutomationProperties.AutomationId="Rm8Function"
ItemsSource="{Binding Source={StaticResource Functions}}"
SelectedItem="{Binding Function, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Mode=OneTime, Converter={StaticResource FunctionEnumConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
即我已使用自定义DataTemplate覆盖ItemTemplate。
但是,现在我无法使用ui自动化选择组合框项目的答案选择项目 :
public static void SelectComboBoxItem(this AutomationElement comboBox, string item)
{
var expandCollapsePattern = comboBox.GetPattern<ExpandCollapsePattern>(ExpandCollapsePatternIdentifiers.Pattern);
expandCollapsePattern.Expand();
expandCollapsePattern.Collapse();
var listItem = comboBox.FindFirst(TreeScope.Subtree,
new PropertyCondition(AutomationElement.NameProperty, item));
var selectionItemPattern = listItem.GetPattern<SelectionItemPattern>(SelectionItemPatternIdentifiers.Pattern);
selectionItemPattern.Select();
}
public static T GetPattern<T>(this AutomationElement element, AutomationPattern pattern) where T: BasePattern
{
try
{
return (T) element.GetCurrentPattern(pattern);
}
catch (InvalidOperationException)
{
element.PrintSupportedPatterns();
throw;
}
}
它会抛出一个错误,告诉我SelectionItemPatternIdentifiers.Pattern
是一个不受支持的模式。 它只是它试图在ComboBox中选择的元素支持的SynchronizedInputPatternIdentifiers.Pattern
。
我应该如何编写DataTemplate以使其成为可选择的?
我用以下方式重新定义了我的ComboBox
:
<ComboBox AutomationProperties.AutomationId="Rm8Function"
ItemsSource="{Binding Source={StaticResource Functions}}"
SelectedItem="{Binding Function, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
AutomationProperties.Name="{Binding Mode=OneTime, Converter={StaticResource FunctionEnumConverter}}"
Text="{Binding Mode=OneTime, Converter={StaticResource FunctionEnumConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
为TextBlock
提供与其Text
值相同的AutomationProperties.Name
值。
我还更新了我的函数,选择ComboBox项目如下:
public static void SelectComboBoxItem(this AutomationElement comboBox, string item)
{
var expandCollapsePattern = comboBox.GetPattern<ExpandCollapsePattern>(ExpandCollapsePatternIdentifiers.Pattern);
expandCollapsePattern.Expand();
expandCollapsePattern.Collapse();
var listItem = comboBox.FindFirst(TreeScope.Subtree,
new PropertyCondition(AutomationElement.NameProperty, item));
var walker = TreeWalker.ControlViewWalker;
var parent = walker.GetParent(listItem);
while (parent != comboBox)
{
listItem = parent;
parent = walker.GetParent(listItem);
}
var selectionItemPattern = listItem.GetPattern<SelectionItemPattern>(SelectionItemPatternIdentifiers.Pattern);
selectionItemPattern.Select();
}
显然,当按原样使用ComboBox
而不覆盖ItemTemplate
,上面的函数会找到它的直接子ItemTemplate
,它是一个ListBoxItem
。 它是可通过SelectionItemPattern
模式选择的ListBoxItem
。 但是当覆盖ItemTemplate
,函数会找到作为ListBoxItem
的子项的TextBlock
。 因此,我必须以这样的方式修改我的函数,使其向上遍历,直到它找到ComboBox
的直接子ComboBox
并选择它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.