我是 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/