首页 > 其他分享 >WPF listbox中添加index

WPF listbox中添加index

时间:2022-10-11 13:34:08浏览次数:70  
标签:Index index RelativeSource 添加 WPF listbox

关键代码:

如果中在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>

标签:Index,index,RelativeSource,添加,WPF,listbox
From: https://www.cnblogs.com/baibaisheng/p/16778875.html

相关文章