Question

I'm totally new to wpf and silverlight, and I have A LOT to learn...

I've got a listbox which contain a template

<ListBox ItemsSource="{Binding itemList}" x:Name="list">
    <ListBox.ItemTemplate>
        <DataTemplate x:Name="datatemplate" >
            <Grid Name="{Binding Id}">
                <TextBlock Text="{Binding Txt}"></TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

i need to assign a AutomationProperties.AutomationId to each list item, like in

<ListBoxItem x:Name="lb_a" AutomationProperties.AutomationId="lb_1">
    <Grid Name="grid_a">
        <TextBlock Text="aa"></TextBlock>
    </Grid>
</ListBoxItem>
<ListBoxItem x:Name="lb_b" AutomationProperties.AutomationId="lb_2">
    <Grid Name="grid_b">
        <TextBlock Text="bb"></TextBlock>
    </Grid>
</ListBoxItem>
...

how can i do? is that even possible?

Was it helpful?

Solution

You can set your attached property in ItemContainerStyle:

<ListBox ItemsSource="{Binding itemList}" x:Name="list">
    <ListBox.ItemTemplate>

    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="AutomationProperties.AutomationId" Value="{Binding MyAutomationIdProperty}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top