【问题标题】:Data bind the index of an item in a listbox?数据绑定列表框中项目的索引?
【发布时间】:2012-03-11 00:17:09
【问题描述】:

我有一组本地高分 (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 bind to index of the item

【问题讨论】:

    标签: windows-phone-7 listbox


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2011-05-18
      • 2015-04-04
      • 2011-11-17
      • 2019-02-20
      相关资源
      最近更新 更多