I search for some way to show the items index in a ItemsControl
that is using DataTemplate
. So I found this good question. I used the idea in that question but all the values are zero! The only different between my code and the code in that question is that my controls (that is going to show the index) is not directly in the DataTemplate
. It is in a Grid
and the Grid
is in the DataTemplate
Here is my code:
<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>
Result:
0
0
0
// and so on
What I expect to be shown:
0
1
2
// and so on
What is wrong whit this code?
The AlternationCount
property of your property needs to be set.
<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>
</ItemsControl>