我正在寻找某种方式来显示正在使用ItemsControl
的DataTemplate
中的项目索引。因此,我发现了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
属性。
<ItemsControl ItemsSource="{Binding }" AlternationCount={Binding CountOfItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
// column definitions
<Label Content="{Binding RelativeSource={RelativeSource
Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}"
/>
// some other controls
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>