WPF – Bind to Item Index from within ItemTemplate of ItemsControl?

If you’re not using any type of alternating row styles you might be able to hijack the AlternationIndex for this. Set AlternationCount on your ItemsControl to something greater than the max possible count of your items and then use

Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}"

Edit: As bradgonesurfing pointed out in comments, this is not recommended if you’re using virtualization, as it will only index the items that are generated and not the entire list.

Leave a Comment

tech