更新时间:2023-11-17 08:37:10
我建议看:
WPF ItemsControl ItemsSource 中的当前 ListItem 索引一个>
它解释了如何解决 ItemsControl 上没有内置 Index 属性这一事实.
It explains how to work around the fact that there isn't a built in Index property on the ItemsControl.
我尝试了以下代码:
<Window.Resources>
<x:Array Type="{x:Type sys:String}" x:Key="MyArray">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
<sys:String>Three</sys:String>
</x:Array>
</Window.Resources>
<ItemsControl ItemsSource="{StaticResource MyArray}" AlternationCount="100" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex),
RelativeSource={RelativeSource TemplatedParent},
StringFormat={}Index is {0}}">
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl >
并获得一个包含三个 TextBlock 的窗口,例如:
And get a window with three TextBlocks like:
[Index is 0]
[Index is 1]
[Index is 2]