【发布时间】:2017-04-06 04:45:11
【问题描述】:
我在 ToolBar 中使用 ToggleButtons,我想在 UI 自动化测试中获取和使用它们,但是当我检查这些按钮的 AutomationElement.Current 时,它的 ClassName 属性是 Button,而我希望是 ToggleButton
xaml 不是starrightforward,所以我在这里提一下:
<ToolBar ItemsSource="{Binding}"/>
对于 ItemsSource 中的类型,我有一个 DataTemplate:
<DataTemplate DataType="{x:Type myViewModelType}">
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource MyToolBarElementTemplate}">
<ContentPresenter.Resources>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource ThisStyleSetsWidthAndHeight}"/>
</ContentPresenter.Resources>
</ContentPresenter>
</DataTemplate>
样式定义如下:
<Style TargetType="{x:Type ButtonBase}" x:Key="ThisStyleSetsWidthAndHeight">
<Setter Property="styles:AttachedProperties.ContentWidth" Value="32"/>
<Setter Property="styles:AttachedProperties.ContentHeight" Value="32"/>
</Style>
内容模板如下所示:
<DataTemplate x:Key="MyToolBarElementTemplate" DataType="{x:Type myViewModelType}">
<ToggleButton x:Name="AutomationIdThatIGetOk">
...
</ToggleButton>
</DataTemplate>
我对自动化框架有点陌生,我想它与所有这些模板和样式有关,但是有没有办法为这个 ToggleButton 创建正确的 AutomationPeer 实例?
【问题讨论】:
标签: c# wpf xaml user-interface ui-automation