简体   繁体   English

将属性绑定到创建的每个ListBox.ItemTemplate

[英]Bind property to each ListBox.ItemTemplate created

I'm totally new to wpf and silverlight, and I have A LOT to learn... 我是wpf和silverlight的新手,我有很多东西可以学习...

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 我需要为每个列表项分配一个AutomationProperties.AutomationId ,例如

<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? 那有可能吗?

You can set your attached property in ItemContainerStyle : 您可以在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM