AutomationID and Templates  
Author Message
Elia Noris





PostPosted: Microsoft UI Automation, AutomationID and Templates Top

I want to access elements which are dinamically generated through a template:

I define the following ListBox

<ListBox
ItemsSource="{Binding Source={StaticResource contactsView}}"
HorizontalAlignment="Right"
Margin="0,100,70,160"
Name="listBox1"
AutomationProperties.AutomationId="ContactsList">
</ListBox>


and the following template

<Window.Resources>
<DataTemplate DataType="{x:Type src:Contact}">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Stretch"
>
<TextBlock Text="{Binding Path=Name}"
Width="70"
AutomationProperties.AutomationId="ContactName"/>
<Button Content="Call"
Margin="3,3,3,3"
Click="OnContactClick"
AutomationProperties.AutomationId="ContactCall"/>
</StackPanel>
</DataTemplate>
<CollectionViewSource Source="{Binding Source={x:Static Application.Current}, Path=Contacts}" x:Key="contactsView"/>
</Window.Resources>


then I populate the listBox.


Is there a way to dinamically create AutomationIDs for the template generated ListBoxItems

Or what else would you suggest to access these elements


Software Development for Windows Vista19  
 
 
Peter Donnelly - MSFT





PostPosted: Microsoft UI Automation, AutomationID and Templates Top

Have you tried setting the AutomationID via the ItemContainerStyle You can just use a setter in the ItemContainerStyle for AutomationProperties.AutomationID.

Keep in mind, these IDs will only appear for “realized” items—those that have been scrolled into view.



 
 
jasona42





PostPosted: Microsoft UI Automation, AutomationID and Templates Top

I have this same problem and it is frustrating me to no end. I have spent many hours researching trying to find a work-around and your post is the first time I've seen anyone else describe this problem anywhere.

Please tell me you've found some sort of solution that works, our automation team would be very happy if we could fix this. :)