我有一组本地高分(ObservableCollection),它们是绑定到ListBox的数据,所以我可以显示它们。这是我目前所拥有的,第一个TextBlock的"{Binding Index}"是我试图获取当前项的索引的地方,但我不完全确定通过数据绑定是否可能:
<ListBox Grid.Row="1" x:Name="localScoresListBox" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Index}" Grid.Column="0" />
<TextBlock Text="{Binding PlayerName}" Grid.Column="1" />
<TextBlock Text="{Binding Score}" Grid.Column="2" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
所以我的问题。使用数据绑定,可以获取当前项的索引吗?
如果可能的话,我相信它将是基于0的,因为我希望第一个秩是1而不是0,你能做一些类似于{Binding Index+1}的事情吗?
编辑:WPF也有类似的问题,当时似乎也没有办法做到:WPF ListBox绑定到项目的索引
据我所知,尤其是因为绑定的项将是对象的任何类型,而且它可能没有Index属性。
我建议将index属性添加到数据对象中(在填充集合时可以添加/操作它)。