XAML - binding row and column index of cell to automation ID

I'm in the process of providing automation IDs to individual cells in a datagrid WPF document, but I hit a bit. I decided to try to name the cells according to their position in the grid (row index and column index). Using the user interface inspector and selecting one of the DataGridCells in question, you will see the following properties:

GridItem.Row: 2 GridItem.Column: 0

... which makes me think that I can access these properties through binding. However, I have spent most of the past few days combing the Internet, how to do it, but have not found anything.

The current XAML code is as follows ("???" 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> 

Is there such a way to these properties? Or is there another method for providing unique automation identifiers to individual cells? I am not very experienced with WPF and XAML, so any pointers are appreciated.

Thanks in advance.

+6
source share
2 answers

Got it to work at last. The shipping solution is here so others can take advantage.

Code behind (based on 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 

And 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> 
+6
source

Ok, I checked it (not with a datagrid, but with a grid, it should be the same) and it works:

 <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> 
+1
source

Source: https://habr.com/ru/post/906948/


All Articles