How to update views when underlying model changes #17762
-
Hello, I have a mainViewModel which instantiates a subViewModel. The subViewModel works with an underlying model. You can set the reference to the model through a public property of the subViewModel. When the MainViewModel updates that public property of the subViewModel (it sets the model reference to a different model object), that SubViewModel's view doesn't get "triggered" to update all its bound properties. It's only when I click to another tab and then come back to that tab (view) again, that the subViewModel will call all its getters (which now refer to a different model object). How can I make that happen as soon as the public property is set on the subViewModel? Here is some code to explain: Main View Model:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Usually you set the DataContext to the correct sub view model (assuming they also implement Your view get's the MainViewModel as a DataContext. The control which needs to bind to the In some situations you can't do that easily or if other properties of the control with the "sub" view model data context needs to be bound to the main view model, you may need to go up using $parent... to do the binding. Alternatively, you monitor changes in the main view model for the sub view model (using reactive or subscribing to NotifyPropertyChanged and update properties this way. Unfortunately, AFAIK, there's no built-in/automatic way to deal with this more elegantly. |
Beta Was this translation helpful? Give feedback.
-
This is looking like the good old master-detail pattern. See an example here for UWP but this should transfer easily to Avalonia. |
Beta Was this translation helpful? Give feedback.
-
The following worked for me, if anyone is interested. I used inversion of control to accomplish the task. Each sub-viewModel is passed an instance of the MainViewModel in its constructor. So in the MainViewModel constructor we have the following
Then, inside each subviewmodel, we subscribe to the 'propertyChanged' event in the MainViewModel (in this case the event is that the Model changed). MainViewModel is a ReactiveObject.
Inside of each subviewmodule needs to be a property like this:
Thus, when the underlying model is changed to refer to a different object, all the subviewModels that are subscribed to that event will receive notification. Then they can update all their bound properties and their respective Views will show the updates. |
Beta Was this translation helpful? Give feedback.
The following worked for me, if anyone is interested.
I used inversion of control to accomplish the task. Each sub-viewModel is passed an instance of the MainViewModel in its constructor. So in the MainViewModel constructor we have the following
Then, inside each subviewmodel, we subscribe to the 'propertyChanged' event in the MainViewModel (in this case the event is that the Model changed). MainViewModel is a ReactiveObject.