关键代码:
如果中在ItemsControl中加入Index,"RelativeSource={RelativeSource AncestorType=ListBoxItem}"可以写成,"RelativeSource={RelativeSource TemplatedParent}"但是ListBox中这样便不会有效果。
<ListBox
x:Name="lstBox"
Height="200"
AlternationCount="100000"
ItemsSource="{Binding Logs}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex)}" />
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>