WPF ToggleButton的AutomationElement - 具有ClassName Button

时间:2017-04-05 11:42:19

标签: c# wpf xaml user-interface ui-automation

我在ToolBar中使用ToggleButtons,我想在UI自动化测试中使用它们,但是当我检查AutomationElement.Current这些按钮时,它的ClassName属性是Button,而我期望ToggleButton

xaml不是明星向前,所以我在这里提到它:

<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>

我对Automation Framework有点新,我想它与所有这些模板和样式有关,但有没有办法为这个ToggleButton创建正确的AutomationPeer实例?

1 个答案:

答案 0 :(得分:1)

  

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

您的期望是错误的,因为$start = $_REQUEST['iDisplayStart']; // to handle pagination $length = $_REQUEST['iDisplayLength']; // to handle pagination $sSearch = $_REQUEST['sSearch']; // for searching $col = $_REQUEST['iSortCol_0']; $arr = array(0 => 'id', 1 => 'first_name', 2 => 'last_name', 3 => 'email'); $sort_by = $arr[$col]; $sort_type = $_REQUEST['sSortDir_0']; $qry = "select id, first_name, last_name, email, position, office from datatables_demo where (first_name LIKE '%".$sSearch."%' or last_name LIKE '%".$sSearch."%' or email LIKE '%".$sSearch."%') ORDER BY ".$sort_by." ".$sort_type." LIMIT ".$start.", ".$length; $res = mysql_query($qry); while($row = mysql_fetch_assoc($res)) { $data[] = $row; } $qry = "select count(id) as count from datatables_demo"; $res = mysql_query($qry); while($row = mysql_fetch_assoc($res)) { $iTotal = $row['count']; } $rec = array( 'iTotalRecords' => $iTotal, 'iTotalDisplayRecords' => $iTotal, 'aaData' => array() ); $k=0; if (isset($data) && is_array($data)) { foreach ($data as $item) { $rec['aaData'][$k] = array( 0 => $item['id'], 1 => '<span id="'.$item['id'].'" name="first_name" class="editable">'.$item['first_name'].'</span>', 2 => '<span id="'.$item['id'].'" name="last_name" class="editable">'.$item['last_name'].'</span>', 3 => '<span id="'.$item['id'].'" name="email" class="editable">'.$item['email'].'</span>', 4 => '<span id="'.$item['id'].'" name="position" class="editable">'.$item['position'].'</span>', 5 => '<span id="'.$item['id'].'" name="office" class="editable">'.$item['office'].'</span>' ); $k++; } } echo json_encode($rec); 类实际上从其ToggleButtonAutomationPeer方法返回string“按钮”:https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Automation/Peers/ToggleButtonAutomationPeer.cs,a58abe77888c16cd

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