Numbered listbox

后端 未结 5 416
闹比i
闹比i 2020-11-28 09:11

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

5条回答
  •  眼角桃花
    2020-11-28 09:35

    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.

提交回复
热议问题