WPF listBox 顯示每個Item的index值
在顯示出來的Item中顯示出當前Item的index值
Xaml檔案如下:
<Window.Resources>
<Style x:Key="wrapalListBox" TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="listItemTempalte">
<Button Width="40" Height="30" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplatedParent.(ItemsControl.AlternationIndex)}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Style="{ StaticResource wrapalListBox}" ItemTemplate="{StaticResource listItemTempalte}" ItemsSource="{Binding Items}" AlternationCount="{Binding Path=Items.Count}" >
</ListBox>
</Grid>
後臺程式碼中給一個List型別的Items屬性 呈現的效果如圖
參考http://stackoverflow.com/questions/745568/numbered-listbox/748383