2012-01-26 20 views
6

我正在爲WPF數據網格中的單個單元格提供自動化ID,但我遇到了一些障礙。我決定嘗試根據它們在網格中的位置(行索引和列索引)命名單元格。使用UI檢查,並強調有問題的DataGridCells的一個表現出以下性能:XAML - 將單元格的行和列索引綁定到自動化ID

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不太瞭解,所以任何指針都會被讚賞。

在此先感謝。

+0

我不知道,但嘗試<綁定路徑= 「GridItem.Column」 的RelativeSource = { RelativeSource Mode = Self} /> – Eyjafjallajokull

+0

嘗試插入你的代碼片段,但不幸的是它沒有產生正確的結果(automationId由於某種原因是空白的)。感謝您的迴應 - 如果我偶然發現某些東西,我會繼續玩弄東西併發布。 – CSD

回答

6

得到了它的最後工作。在此發佈解決方案,以便其他人可能受益。

背後的代碼(基於關閉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> 
1

好吧,我檢查了它(不與數據網格,但與網格,它應該是相同的),以及它的工作原理:

<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-0」作爲網格中所有單元的自動化ID。與我之前所做的相比,這是一大進步 - 感謝您的幫助! – CSD

+0

查看DataGrid和Grid類的其他一些屬性...嘗試了'但是所有單元格都接收到-1。 – CSD

+0

你能告訴我你的屬性「GridItem.Row」和「GridItem.Column」來自哪裏?我用datagrid創建了測試項目來使用Snoop來檢查這個問題,但我沒有看到這些屬性。 – Eyjafjallajokull