How to display row numbers in a ListView?

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

    If you have a dynamic list where items are added, deleted or moved, you can still use this very nice solution and simply let the currentview of your listview refresh itself after the changements in your source list are done. This code sample removes the current item directly in the data source list "mySourceList" (which is in my case an ObservableCollection) and finally updates the line numbers to correct values .

    ICollectionView cv = CollectionViewSource.GetDefaultView(listviewNames.ItemsSource);
    if (listviewNames.Items.CurrentItem != null)
    {
        mySourceList.RemoveAt(cv.CurrentPosition);
        cv.Refresh();
    }
    

提交回复
热议问题