I have such DataGrid
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.
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.
The way I've set up a updating lists in the past is:
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
.
In you model_DataArrived
method in the ViewModel find all matching objects and use the Update
method from step 1 to update the objects.
Find all new objects and add them to you list (DataList
).
Find all missing objects and remove them from you list (DataList
).