c# - 将属性绑定(bind)到创建的每个 ListBox.ItemTemplate

标签 c# silverlight xaml silverlight-5.0 listboxitem

我是 wpf 和 silverlight 的新手,我还有很多东西要学...

我有一个包含模板的列表框

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

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

我该怎么办? 这可能吗?

最佳答案

您可以在 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>

关于c# - 将属性绑定(bind)到创建的每个 ListBox.ItemTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288109/

相关文章:

c# - Xaml 和事件不起作用

c# - .net mvc JWT token 更改 utf-8 字符

c# - 使用 IE 而不是默认浏览器打开 url

c# - 自定义列表的 indexof 函数

c# - 为什么 AmazonS3Util.DoesS3BucketExist 始终返回 true?

c# - ListPicker 数据绑定(bind)和 INotifyPropertyChanged

Silverlight 运行时错误 1001

vb.net - 从流生成字节数组

wpf - 如何在 WPF 中动态添加详细信息行?

windows-phone-7 - 未经授权的访问异常未处理