0

I want to update the data on drop-down in a view from my view-model. The data is updated in some objects in my model. I understood that the INotifyPropertyChanged interface only works on view-model level. But I thought when the view-model subscribe to an event that I can update my view by raising the RaisePropertyChanged for the object in the view-model which needs to be updated.

I call the UpdateConfiguration() which triggers to fetch the new data. Becacuse the function PutSetupWorkspaceConfiguration() is time consuming it returns the the first data and then compute the rest in the background. After its done its rasing the IsChanged event which works. So the method OnListChanged() gets called but then the gui does not update.

What am I doing wrong since the event and the getter gets called?

/*viewmodel*/
private SetupWorkspaceConfiguration setupWorkspaceConfiguration;

public ObservableCollection<TestingModel> TestingModels
{
   get => setupWorkspaceConfiguration?.TestingModels; //The event calls getter but the gui does not update the data
}

public async Task UpdateConfiguration(int? projectId)
{
   setupWorkspaceConfiguration = await Task.Run(async () => await setupWorkspaceConfigurationStore.PutSetupWorkspaceConfiguration(projectId));            
   setupWorkspaceConfiguration.IsChanged -= OnListChanged;
   setupWorkspaceConfiguration.IsChanged += OnListChanged;
}
private void OnListChanged(object? sender, System.EventArgs e)
{
    RaisePropertyChanged(nameof(TestingModels));
}

EDIT: The elements in the list setupWorkspaceConfiguration.TestingModels will updated. This is working fine. But when i add new elements to the list this will not be shown in the dropdown.

EDIT2: The Task.Run seems to cause the problem. But why? Tasks can not be really the problem because If I call Task.Run for UpdateConfiguration() it still works and as well if I make a Task.Run in PutSetupWorkspaceConfiguration()

    SetupWorkspaceConfiguration = await Task.Run(async () => await setupWorkspaceConfigurationStore.PutSetupWorkspaceConfiguration(projectId)); //does not work
    SetupWorkspaceConfiguration = await setupWorkspaceConfigurationStore.PutSetupWorkspaceConfiguration(projectId); //does work
user3077796
  • 192
  • 1
  • 19
  • 1
    `the gui does not update` means what exactly? Do you have a new list with different objects in it after the update? Or do you have the same list with the same objects but changed properties on those objects? Your `RaisePropertyChanged` covers only the former case, in the latter case, each of the objects has to `RaisePropertyChanged` for each of its changed properties. – Haukinger Feb 26 '22 at 17:23
  • https://gist.github.com/KMastalerz/44316423d1b582e73ecaf8b563e58e6a Some quick excample of MVVM, although im not an expert i think this may help. –  Feb 26 '22 at 17:27
  • @Haukinger: The list is the same but the properties in the list are changing (which is working fine) but new elements which gets added to the list will not shown in the dropdown. – user3077796 Feb 26 '22 at 18:03
  • @KrzysiekMastalerz: Thanks for the link. Do you have more examples because this covers only if you a view and a view-model. There everything works fine for me as well. But when an element to the TestingModels list gets added in my model the gui does not update the dropdown even so the getter is called. – user3077796 Feb 26 '22 at 18:15
  • https://github.com/KMastalerz/TestMVVM 1st you have to update on correct thread. 2nd this thread has to be free to update UI. –  Feb 26 '22 at 19:25
  • @KrzysiekMastalerz: You are right it has something to do with threads (see EDIT2) But I thought that RaisePropertyChanged will invoke the UI Thread. Is that not the case? – user3077796 Feb 26 '22 at 20:34
  • @KrzysiekMastalerz when _modifying_ a collection, one has to be on the right thread, when updating a regular property (including _replacing_ a collection), the change notification gets marshalled automatically. – Haukinger Feb 27 '22 at 12:05
  • I think, you don`t need an RaisPropertyChanged. What is missing is a CollectionChanged event. Possibly this question (or better the answer) can help you https://stackoverflow.com/questions/7449196/ – h.m.i.13 Jun 06 '22 at 15:25

0 Answers0