2012-03-11 63 views
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> 

所以我的問題。使用數據綁定,你可以獲取當前項目的索引嗎?

如果有可能,我相信它會是基於0的,因爲我希望第一個排名是1而不是0,你可以做一些類似{綁定索引+1}的事情嗎?

編輯:這裏有一個類似的問題WPF和似乎沒有成爲一個辦法做到這一點,那麼:WPF ListBox bind to index of the item

回答

0

不,我知道的 - 尤其是因爲您綁定將是該項目無論你的對象是什麼類型,它可能沒有索引屬性。

我建議將索引屬性添加到您的數據對象(當您填充集合時可以添加/操作它)。

相關問題