XAML - 将单元格的行和列索引绑定到自动化 ID [英] XAML - Binding row and column index of cell to automation ID

查看:22
本文介绍了XAML - 将单元格的行和列索引绑定到自动化 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm in the process of giving automation IDs to individual cells within a WPF datagrid, but I've hit a bit of a snag. I've decided to try naming the cells according to their position in the grid (row index and column index). Using a UI inspector and highlighting one of the DataGridCells in question shows the following properties:

GridItem.Row: 2GridItem.Column: 0

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

... which leads me to believe that I can access these properties via binding. However, I've spent the better part of the last few days combing the Internet for how to go about this, but haven't found anything.

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

The current XAML code is as follows (the '???' are placeholders):

<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 不是很熟悉,因此不胜感激.

Does such a path to these properties exist? Or does another method exist for giving unique automation IDs to individual cells? I'm not very experienced with WPF and XAML so any pointers are appreciated.

提前致谢.

推荐答案

终于可以使用了.在此处发布解决方案,以便其他人受益.

Got it to work finally. Posting solution here so that others may benefit.

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

The code behind (based off 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>

这篇关于XAML - 将单元格的行和列索引绑定到自动化 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆