WPF listbox中添加index

关键代码:

如果中在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>
posted @ 2022-10-11 13:15  潜谷先生  阅读(129)  评论(0)    收藏  举报