【问题标题】:Overriding DataGridRow style for CodedUI testing produces undesirable appearance覆盖 CodedUI 测试的 DataGridRow 样式会产生不良外观
【发布时间】:2015-05-19 08:37:49
【问题描述】:

由于 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;
    }

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

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

【问题讨论】:

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


    【解决方案1】:

    有一个 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>
    

    【讨论】:

    • 我肯定需要在某个地方定义一个“DataGridRowStyle”吗?我真的不知道这是从哪里来的?
    • 我假设你有一个风格,但是一旦你在你的例子中添加了那些 setter,它就丢失了。但你实际上失去了默认行为?
    • 我失去了默认行为,是的。我确实定义了默认样式,但它没有 x:Key(因此所有其他 DataGridRows 默认为该样式)。如果我添加 x:Key 和 BasedOn,它适用于我当前的 DataGrid,但所有其他的都不起作用!
    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多