2013-01-19 107 views
1

我遇到問題。我必將ListViewObservableCollection<ImageSource>綁定到ObservableCollection的索引

<ListView x:Name="FramesListView" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Disabled" 
     ItemsSource="{Binding Path=FrameImages}" SelectionChanged="FramesListView_OnSelectionChanged"> 
<ListView.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel 
      Width="Auto" 
      ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" 
      ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" /> 
    </ItemsPanelTemplate> 
</ListView.ItemsPanel> 

<ListView.ItemTemplate> 
    <DataTemplate> 
     <Border Width="100" Height="75" BorderThickness="1" BorderBrush="DarkGray" VerticalAlignment="Center" Margin="7,5,7,5"> 
      <StackPanel Orientation="Vertical"> 
       <Image Margin="5,5,5,5" Width="100" Height="75" Source="{Binding}" Stretch="Fill"></Image> 
       <Label HorizontalAlignment="Center" Content="{Binding}"></Label> 
      </StackPanel> 
     </Border> 
    </DataTemplate> 
</ListView.ItemTemplate> 
</ListView> 

一切工作正常,除了一兩件事。我也想在每張圖片下顯示索引。我不知道如何實現這一點。

如何更改該行以使其成爲可能?

<Label HorizontalAlignment="Center" Content="{Binding}"></Label> 

回答

2

我曾經在類似的情況下使用過一個有點不好的解決方案,就是爲了這個目的使用了AlternationIndex屬性。通過將AlternationCount設置爲足夠高的數字,可以將AlternationIndex屬性用作計數器。

<ItemsControl x:Name="itemsControl" AlternationCount="100000"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Label Content="{Binding Path='(ItemsControl.AlternationIndex)', RelativeSource={RelativeSource AncestorType=ContentPresenter}}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

然而,這具有的缺點是開始編號從0

另一個清潔的解決方案包括創建ValueConverter如這裏討論: How can I bind against the Index of a ListBoxItem

相關問題