我正在寻找一种方法,在使用 DataTemplate 的 ItemsControl 中显示项目索引。所以我发现了这个好问题this。我使用了那个问题中的思路,但是所有的值都是零!唯一不同的是我的控件(将要显示索引)不直接在 DataTemplate 中。它在一个 Grid 中,而 Grid 在 DataTemplate 中。
以下是我的代码:
我希望显示的内容:
这段代码有什么问题?
<ItemsControl ItemsSource="{Binding }">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
// column definitions
<Label Content="{Binding RelativeSource={RelativeSource
Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}"/>
// some other controls
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
结果:
0
0
0
// and so on
我希望显示的内容:
0
1
2
// and so on
这段代码有什么问题?
AlternationCount="1000"
,它就应该可以工作了。 - Florian Gl