【问题标题】:XAML - Binding row and column index of cell to automation IDXAML - 将单元格的行和列索引绑定到自动化 ID
【发布时间】:2016-11-21 21:49:09
【问题描述】:

我正在为 WPF 数据网格中的单个单元格提供自动化 ID,但我遇到了一些障碍。我决定尝试根据它们在网格中的位置(行索引和列索引)来命名单元格。使用 UI 检查器并突出显示有问题的 DataGridCell 之一会显示以下属性:

GridItem.Row: 2 GridItem.Column: 0

... 这让我相信我可以通过绑定访问这些属性。但是,我在过去几天的大部分时间里都在网上搜索如何解决这个问题,但没有找到任何东西。

当前的 XAML 代码如下('???' 是占位符):

<DataGrid.CellStyle>
  <Style TargetType="{x:Type DataGridCell}">
    <Setter Property="AutomationProperties.AutomationId">
      <Setter.Value>
        <MultiBinding StringFormat="cell:{0}-{1}">
          <Binding ??? />
          <Binding ??? />
        </MultiBinding>
      </Setter.Value>
    </Setter> 
  </Style>
</DataGrid.CellStyle>

这些属性的路径是否存在?或者是否有另一种方法可以为单个单元格提供唯一的自动化 ID?我对 WPF 和 XAML 不是很有经验,所以任何指针都值得赞赏。

提前致谢。

【问题讨论】:

  • 我不确定,但试试
  • 尝试插入您的 sn-p 但不幸的是它没有产生正确的结果(无论出于何种原因,automationId 为空白)。感谢您的回复 - 如果我偶然发现某些东西,我会继续玩弄东西并发布。

标签: xaml


【解决方案1】:

终于搞定了。在此处发布解决方案,以便其他人受益。

背后的代码(基于http://gregandora.wordpress.com/2011/01/11/wpf-4-datagrid-getting-the-row-number-into-the-rowheader/):

Private Sub DataGrid_LoadingRow(sender As System.Object, e As System.Windows.Controls.DataGridRowEventArgs)
  e.Row.Tag = (e.Row.GetIndex()).ToString()
End Sub

还有 XAML:

<DataGrid ... LoadingRow="DataGrid_LoadingRow" >

<DataGrid.ItemContainerStyle>
  <Style TargetType="{x:Type DataGridRow}">
    <Setter Property="AutomationProperties.AutomationId">
      <Setter.Value>
        <MultiBinding StringFormat="Row{0}">
          <Binding Path="(DataGridRow.Tag)"
                   RelativeSource="{RelativeSource Mode=Self}" />
        </MultiBinding>
      </Setter.Value>
    </Setter>
    <Setter Property="AutomationProperties.Name">
      <Setter.Value>
        <MultiBinding StringFormat="Row{0}">
          <Binding Path="(DataGridRow.Tag)"
                   RelativeSource="{RelativeSource Mode=Self}" />
        </MultiBinding>
      </Setter.Value>
    </Setter>
  </Style>
</DataGrid.ItemContainerStyle>

...

<DataGrid.CellStyle>
  <Style>
    <Setter Property="AutomationProperties.AutomationId">
      <Setter.Value>
        <MultiBinding StringFormat="cell{0}Col{1}">

          <!-- bind to row automation name (which contains row index) -->
          <Binding Path="(AutomationProperties.Name)"
                   RelativeSource="{RelativeSource AncestorType=DataGridRow}" />

          <!-- bind to column index -->
          <Binding Path="(DataGridCell.TabIndex)"
                   RelativeSource="{RelativeSource Mode=Self}" />

        </MultiBinding>
      </Setter.Value>
    </Setter> 
  </Style>
</DataGrid.CellStyle>

...

</DataGrid>

【讨论】:

    【解决方案2】:

    好的,我已经检查过了(不是用datagrid,而是用grid,应该是一样的),它可以工作:

    <AutomationProperties.AutomationId>
        <MultiBinding StringFormat="{}{0} - {1}">
                 <Binding Path="(Grid.Row)" RelativeSource="{RelativeSource Mode=Self}" />
                 <Binding Path="(Grid.Column)" RelativeSource="{RelativeSource Mode=Self}" />
            </MultiBinding>
    </AutomationProperties.AutomationId>
    

    【讨论】:

    • 我现在将“0-0”作为网格中所有单元格的自动化 ID。与我以前相比,这是向前迈出的一大步 - 感谢您的帮助!
    • 查看了 DataGrid 和 Grid 类的一些其他属性...尝试了&lt;Binding Path="(DataGrid.SelectedIndex)" RelativeSource="{RelativeSource Mode=Self}" /&gt;,但所有单元格都收到了 -1。
    • 你能告诉我你的属性“GridItem.Row”和“GridItem.Column”在哪里吗?我已经使用数据网格创建了测试项目来使用 Snoop 检查这个问题,但我看不到这些属性。
    • 我成功找到了列的索引( ),但我没有找到行索引。
    • 我使用的是 Active Accessibility Object Inspector (INSPECT),但已经下载了 Snoop,因为它看起来是最新的。我也试图找到这些属性,但没有找到任何东西。我开始认为 GridItem.Row 和 GridItem.Column 可能只是在 INSPECT 中显示的额外信息(我查看了 DataGridCell 和 DataGrid 的 MSDN 文档,这些类都没有任何 GridItem 属性)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多