AutomationID and Templates

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?

[3739 byte] By [EliaNoris] at [2008-2-10]
# 1

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.

PeterDonnelly-MSFT at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Microsoft UI Automation...
# 2
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. :)

jasona42 at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Microsoft UI Automation...

Software Development for Windows Vista

Site Classified