Share via

AutomationID for ContentControl

Sorina P 1 Reputation point
2021-02-02T11:09:00.967+00:00

I used for Datagrid row the next code to implement AutomationId and it worked ok:

LoadingRow="ItemsDataGrid_LoadingRow"
private void ItemsDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Tag = e.Row?.GetIndex().ToString();
}
In XAML:

<DataGrid.ItemContainerStyle>
<Style BasedOn="{StaticResource {x:Type DataGridRow}}" 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>

How Can I access TextBlock using AutomatioId? TextBlock is inside a stackPanel(in this particularly example, TextBlock is inside StackPanel witch is inside a Grid)? Bellow it's a small example of what I have tried until now for Group.
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander
Margin="0,0,-2,0"
DragLeave="Expander_DragLeave"
Drop="Expander_Drop"
FontFamily="Segoe UI"
Background="LightBlue"
GiveFeedback="Expander_GiveFeedback"
IsExpanded="{Binding Items[0].IsExpanded}"
Loaded="Expander_Loaded"
PreviewMouseLeftButtonDown="Expander_PreviewMouseLeftButtonDown"
PreviewMouseMove="Expander_PreviewMouseMove">
<Expander.Header>
<Grid
Name="grid"
HorizontalAlignment="Stretch"
MouseDown="Grid_MouseDown">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="" />
<ColumnDefinition Width="
" />
</Grid.ColumnDefinitions>
<Grid.ContextMenu>
<ContextMenu >
<MenuItem
Click="DeleteGroup_Click"
FontFamily="Segoe UI"
Header="Delete Group">
</MenuItem>
</ContextMenu>
</Grid.ContextMenu>
<TextBlock
HorizontalAlignment="Left"
FontFamily="Segoe UI"
FontWeight="Bold"
Text="{Binding Name}" />
<StackPanel
Grid.Column="1"
HorizontalAlignment="Right"
Orientation="Horizontal"
TextBlock.FontFamily="Segoe UI">
<TextBlock
Margin="5,0,0,0"
FontFamily="Segoe UI"
Text="{Binding Path=ItemCount, Mode=OneWay}" />
<TextBlock
Margin="5,0,0,0"
FontFamily="Segoe UI"
>
Example(s)
</TextBlock>

                            </StackPanel>
                        </Grid>
                    </Expander.Header>
                    <ItemsPresenter />
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="AutomationProperties.AutomationId">
        <Setter.Value>
            <MultiBinding StringFormat="CH_{0}">
                <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=DataGrid}" Path="Tag" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
    <Setter Property="AutomationProperties.Name">
        <Setter.Value>
            <MultiBinding StringFormat="CH_{0}">
                <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=DataGrid}" Path="Tag" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>
Developer technologies | Windows Presentation Foundation

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.