How to display row numbers in a ListView?

前端 未结 10 912
盖世英雄少女心
盖世英雄少女心 2020-11-27 04:33

The obvious solution would be to have a row number property on a ModelView element, but the drawback is that you have to re-generate those when you add records or change sor

10条回答
  •  孤街浪徒
    2020-11-27 05:22

    I think you have the elegant solution, but this works.

    XAML:

    
      
        
          
            
            
          
        
      
    
    

    ValueConverter:

    public class IndexConverter : IValueConverter
    {
        public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
        {
            ListViewItem item = (ListViewItem) value;
            ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
            int index = listView.ItemContainerGenerator.IndexFromContainer(item);
            return index.ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

提交回复
热议问题