Nested ViewModels and Models? #16280
Replies: 1 comment
-
If you are dealing with unknown levels of nesting with unknown types, you can use a tree view (and also possibly a view locator) as they support recursive hierarchy data. Avalonia will do a lot of the hard work for you in that case. You just have to load it into the view somehow. EG put everything into an observable collection and a tree view. https://docs.avaloniaui.net/docs/reference/controls/treeview-1 The tree datagrid can also be used and supports some different scenarios. https://docs.avaloniaui.net/docs/reference/controls/treedatagrid/ If you are just dealing with a flat list, a simple observable collection and items control will work fine.
Technically the view model would not contain the view if you are properly following MVVM. But assuming you mean your view model contains a fixed number of child view models, this is fairly easy. Just do something like <ContentControl Content="{CompiledBinding ChildViewModel}">
<ContentControl.DataTemplates>
<!--Define template for ChildViewModel here!-->
</ContentControl.DataTemplates>
</ContentControl>
There are several ways this could be implemented. I typically just do it as a single view to reduce duplication, and just have a mode property on the view model. You can then just create a converter that will enable/disable things on the view based on the current mode state. But if you want to have different views you can either:
Depending on your actual goals though will probably influence how you deal with this. |
Beta Was this translation helpful? Give feedback.
-
Say I have some sort of class that represents a game save or a level format..
How would I setup it so nested data can be accessed in the viewmodel from the view?
For example a level might have several rooms with several layers each containing different types of objects that hold onto sprite data.
Or how about one viewmodel with two views? maybe one for viewing and one for editing (maybe a toolbar that selects sections).
Beta Was this translation helpful? Give feedback.
All reactions