1

I have such DataGrid

<DataGrid AutoGenerateColumns="True" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" ItemsSource="{Binding DataList}" IsReadOnly="True"/>

In my ViewModel I have such field:

public ObservableCollection<ConsoleData> DataList { get; set; }

And such method which is called every second:

private void model_DataArrived(List<ConsoleData> dataList)
{
    DataList.Clear();
    dataList.ForEach(x => DataList.Add(x));
}

Grid displays some real-time data and updates every second.

The problem is - when I select some row in the grid, the selection is reset after a second (when new data is arrived).

I guess probably this is because I Clear DataList every time?

How to solve this problem?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

3 Answers3

2

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.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
Paul C
  • 4,687
  • 5
  • 39
  • 55
  • This is probably the best way. Grab the id of the selected object, re-bind your grid, then re-set the selection. – Adam Rackis Nov 22 '11 at 19:27
  • hmm two-way databinding is so ammazing and high-level feature that i do not understand why should I care about "selection"? it sound like a hack. probably I just using two-way data-binding in a wrong way? – Oleg Vazhnev Nov 22 '11 at 19:35
  • In order to do with with MVVM you'll have to bind the selected item in the view to an property in you ViewModel. When you do the update just record what was selected and when you done loading reset you selected item. – Nick O Nov 22 '11 at 19:36
  • @javapowered this is kind of a Hack. – Nick O Nov 22 '11 at 19:36
  • it's terrible. I learned MVVM, databinding, dependency injection and now I have to "store previous selection and restore it after the data is updated". I hope someone will be able to suggest something better... – Oleg Vazhnev Nov 22 '11 at 19:38
  • @javapowered It is the fact you clear the data first so you either have to do something other than clean as "Adam Rackis suggested, my idea, or a different approach. Maybe at point of update make the data read only if it's not already then just notify the user that there is updated information that said with 1 second updates you might have to re-think GUI. – Paul C Nov 22 '11 at 20:57
1

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.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • no, my viewModel doesn't implements `INotifyPropertyChanged`. i think that probably it's better not to clear `DataList`, probably I should really update every field of every `ConsoleData`..... but how to do that? Jason Dolinger in his video introduces one more ViewModel for that... like "ConsoleDataViewModel", he says that he can add more extra stuff to it (like background color etc.)... probalby I should follow this way.... – Oleg Vazhnev Nov 22 '11 at 19:45
  • @javapowered - You probably want you ViewModel to implement `INotifyPropertyChanged` so that any bindings on the properties in you ViewModel will automatically be updated on the UI. – Nick O Nov 22 '11 at 19:50
  • so far my `ViewModel` is fine :) what benefits is implementing `INotifyPropertyChanged` in ViewModel? – Oleg Vazhnev Nov 22 '11 at 19:55
  • INPC is used to propagate any change in binded property to your View. I would suggest you to use ObservableCollection here and simply add items to it. The change will automatically will be reflected on your UI. And for INPC, do read over it. It's must in MVVM apps. :) – Rohit Vats Nov 22 '11 at 20:05
1

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).

Nick O
  • 3,716
  • 6
  • 38
  • 50
  • so far it's the best approach I have probably. what do you think about creating ConsoleDataViewModel? have you seen Jason Dolinger video? it's what he do at the end... – Oleg Vazhnev Nov 22 '11 at 19:48
  • @javapowered - Not a bad idea then you don't have to implement the `INotifyPropertyChanged` on you object you can do it on the ViewModel and you can also do the update of the properties in the ViewModel as well. Keeps you object a lot cleaner. – Nick O Nov 22 '11 at 19:51