I have a sorted listbox and need to display each item\'s row number. In this demo I have a Person class with a Name string property. The listbox displays a a list of Persons
Finally! If found a way much more elegant and probably with better performance either. (see also Accessing an ItemsControl item as it is added)
We "misuse" the property ItemsControl.AlternateIndex for this. Originally it is intended to handle every other row within a ListBox differently. (see http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx)
1. Set AlternatingCount to the amount of items contained in the ListBox
2. Bind to AlternatingIndex your DataTemplate
...
So this works without a converter, an extra CollectionViewSource and most importantly without brute-force-searching the source collection.