【问题标题】:AutomationElement for WPF ToggleButton - have ClassName ButtonWPF ToggleButton 的 AutomationElement - 具有 ClassName 按钮
【发布时间】: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


    【解决方案1】:

    ...但是当我检查这些按钮的 AutomationElement.Current 时,它的 ClassName 属性是 Button,而我希望是 ToggleButton

    您的期望是错误的,因为ToggleButtonAutomationPeer 类实际上从其GetClassNameCore() 方法返回string“按钮”:https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Automation/Peers/ToggleButtonAutomationPeer.cs,a58abe77888c16cd

    所以你得到了正确的实例。

    【讨论】:

    • 我注意到,我现在使用 HelpText 来识别 propper 按钮,并使用 TogglePattern 来做我想做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多