AutomationId for GroupItem in WPF

  • Thread starter Thread starter Quitty15
  • Start date Start date
Q

Quitty15

Guest
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 used it for groupItem? Bellow it's a small example of what I have tried until now. What kind of event is relevant for groupItem? Or, how could I bind property name to my Automation.Id and Automation.Name property?

<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>

Continue reading...
 
Back
Top