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

经验分享 程序员 微信小程序 职场和发展