Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AlternationIndex returns 0 for all items in ItemsControl?

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?

like image 866
Hossein Narimani Rad Avatar asked Dec 20 '22 09:12

Hossein Narimani Rad


1 Answers

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>

like image 97
Chrisjan Lodewyks Avatar answered Mar 03 '23 21:03

Chrisjan Lodewyks