canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

How to have multiple view linked to the same viewmodel #126

Closed Lethargie1 closed 4 years ago

Lethargie1 commented 4 years ago

I have a decently complex model, that need translating into a proper viewmodel. Maping array of numbers to rgb colors, populating attributes that makes sense for the viewmodel but not the model. Theres different view of that viewmodel that I would like to do, whats the proper way?

for example, I want to do a listbox displaying a simple view of the object, and on the side a detailed view of the selected one. The master/detail sample only show simple items that need no viewmodel in the listbox and I havent found another place where different view are linked to a viewmodel.

canton7 commented 4 years ago

I'd probably write that VM as an object which inherits from PropertyChangedBase, then write two VM's (one for the master, one for the detail) which both expose this VM as a property. Those master/detail views can bind through into this shared VM as needed. This also lets you have detail-specific and master-specific properties.

There's no out-of-the-box support for multiple views per VM, simply because the built-in view location logic can't support it. If you want to write your own view-location logic, implement your own IViewManager (there's an example of this in the samples folder).