覆盖CodedUI测试的DataGridRow样式会产生不合需要的外观

时间:2015-05-19 08:37:50

标签: wpf xaml datagrid styles coded-ui-tests

由于CodedUI存在一些缺点,因此很难在DataGrid中选择项目。我发现的一个解决方法是覆盖ItemContainerStyle,如下所示:

<DataGrid.ItemContainerStyle>
     <Style TargetType="{x:Type DataGridRow}">

        <Setter Property="AutomationProperties.AutomationId">
           <Setter.Value>
              <MultiBinding StringFormat="ArisingID_{0}">
                <Binding Path="(DataGridRow.Tag)" RelativeSource="{RelativeSource Mode=Self}" />
              </MultiBinding>
           </Setter.Value>
        </Setter>

        <Setter Property="AutomationProperties.Name">
           <Setter.Value>
              <MultiBinding StringFormat="ArisingID_{0}">
                 <Binding Path="(DataGridRow.Tag)" RelativeSource="{RelativeSource Mode=Self}" />
              </MultiBinding>
           </Setter.Value>
        </Setter>

     </Style>
</DataGrid.ItemContainerStyle>

然后在后面的代码中使用唯一ID填充每个Row.Tag:

  private void MyDataGrid_OnLoadingRow(object sender, DataGridRowEventArgs e)
    {
        var item = e.Row.Item as IMyViewModel;
        e.Row.Tag = item.UniqueSeqId;
    }

但是,有一个问题是这会覆盖一些&#34;默认样式&#34;数据网格行 - 数据网格上的每个单元格似乎都是可单独选择的,而不是仅仅表现为一行。

将默认样式与这些修改结合起来的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

有一个BasedOn property可以设置你的风格,然后添加你添加的额外的设置器:

<DataGrid.ItemContainerStyle>
    <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource DataGridRowStyle}">

    <Setter Property="AutomationProperties.AutomationId">
       <Setter.Value>
          <MultiBinding StringFormat="ArisingID_{0}">
            <Binding Path="(DataGridRow.Tag)" RelativeSource="{RelativeSource Mode=Self}" />
          </MultiBinding>
       </Setter.Value>
    </Setter>

    <Setter Property="AutomationProperties.Name">
       <Setter.Value>
          <MultiBinding StringFormat="ArisingID_{0}">
             <Binding Path="(DataGridRow.Tag)" RelativeSource="{RelativeSource Mode=Self}" />
          </MultiBinding>
       </Setter.Value>
    </Setter>

 </Style>