首页 文章

数据绑定列表框中项目的索引?

提问于
浏览
0

我有一组本地高分(ObservableCollection),它们是绑定到ListBox的数据,所以我可以显示它们 . 这是我目前所拥有的,第一个TextBlock的“”是我试图获取当前项目的索引,但我不完全确定它是否可以通过数据绑定:

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

所以我的问题 . 使用数据绑定,你能 grab 当前项目的索引吗?

如果有可能我相信它会从0开始,因为我希望第一个等级为1而不是0,你能做一些像{Binding Index 1}这样的事吗?

编辑:有's a similar question here for WPF and there didn'似乎是一种方法,然后:WPF ListBox bind to index of the item

1 回答

  • 0

    不是我所知道的 - 特别是因为您绑定的项目将是您的对象的任何类型,并且它可能没有Index属性 .

    我建议将index属性添加到数据对象中(可以在填充集合时添加/操作它) .

相关问题