How to update views when underlying model changes #17762
Replies: 2 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.
-
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.
All reactions