DataGrid - grid selection is reset when new Data is arrived

后端 未结 3 839
囚心锁ツ
囚心锁ツ 2021-01-25 00:29

I have such DataGrid



        
相关标签:
3条回答
  • 2021-01-25 00:54

    Before clear, pick up the currently selected item (a unique identifier if you have one) then attempt to highlight it again on update and if it's not there anymore just don't highlight annything.

    0 讨论(0)
  • 2021-01-25 00:56

    In case the new dataSource still contains your last selected item and if you are following MVVM pattern. All you need to do is Raise PropertyChanged event for your selecetdItem once data source is reloaded. Make sure your viemModel implements INotifyPropertyChanged interface.

    EDIT

    And in case you don't want to clear your datasource every now and then. Simply, use the ObservableCollection in place of the generic list. It internally implements INotifyCollectionChanged, so any addition or deletion of item in your collection will be reflected on your UI.

    0 讨论(0)
  • 2021-01-25 01:08

    The way I've set up a updating lists in the past is:

    1. Create an Update method in you object (ConsoleData) that you can pass a copy of that object and the object updates itself. The object also needs to implement INotifyPropertyChanged.

    2. In you model_DataArrived method in the ViewModel find all matching objects and use the Update method from step 1 to update the objects.

    3. Find all new objects and add them to you list (DataList).

    4. Find all missing objects and remove them from you list (DataList).

    0 讨论(0)
提交回复
热议问题