How to display row numbers in a ListView?

前端 未结 10 883
盖世英雄少女心
盖世英雄少女心 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:10

    By following best answer solution I found an issue when indexes still not updated after removing/replacing items inside list view. To solve that there is one not so clear hint (I propose to use it in small collections): after executing item removing/replacing you should invoke ObservableCollection(INotifyCollectionChanged).CollectionChanged event with Reset action. This is possible to make with extending existing ObservableCollection, which is ItemsSource or use reflection when this is not possible.

    Ex.

    public class ResetableObservableCollection : ObservableCollection
    {
            public void NotifyReset()
            {
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
    }
    
    
    private void ItemsRearranged() 
    {
        Items.NotifyReset();
    }
    

提交回复
热议问题