C
C#•12mo ago
Cin

MVVM - View model with collection of view models?

Within MVVM, can a view model have a collection of view models? Is it okay to do:
class BasketViewModel
{
public ObservableCollection<ItemViewModel> Items {get; set;}
}
class BasketViewModel
{
public ObservableCollection<ItemViewModel> Items {get; set;}
}
Please assume ItemViewModel exists somewhere in the code base. If this is acceptable, where should this collection be initialised from? The constructor or somewhere else?
12 Replies
Sir Rufo
Sir Rufo•12mo ago
Yes of course
Tinister
Tinister•12mo ago
I don't see a problem with that and I tend to do it myself (e.g. Wanting to bind a list of objects to checkboxes needs some sort of intermediate VM)
Florian Voß
Florian Voß•12mo ago
your example does not require VM for each item, could also set ListView.ItemTemplate to a custom DataTemplate in xaml. Thats what I would prefer tho 🤔 should be easy to avoid dependency between two viewmodels and to only have view depend on viewmodel
Cin
CinOP•12mo ago
I will admit my example was a simple one, just for proof of concept
Tinister
Tinister•12mo ago
Sorry I'm a little confused. Yes a custom <DataTemplate DataType="{x:Type local:ItemViewModel}"> in xaml is how I would do it as well But like, if you want to bind Checkbox.IsChecked for each item, how do you do that other than wrap every item in its own ItemViewModel instance which exposes a boolean for that?
Florian Voß
Florian Voß•12mo ago
<ListView ItemsSource={Binding MyItems}>
<ListView.ItemTemplate>
<DataTemplate>
<Checkbox IsChecked={Binding IsTrue}></Checkbox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView ItemsSource={Binding MyItems}>
<ListView.ItemTemplate>
<DataTemplate>
<Checkbox IsChecked={Binding IsTrue}></Checkbox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public ObservableCollection<MyItem> MyItems { get => _myItems; set{ _myItems = value; OnPropertyChanged();}}
public ObservableCollection<MyItem> MyItems { get => _myItems; set{ _myItems = value; OnPropertyChanged();}}
public class MyItem{
public bool IsTrue { get; set; }
}
public class MyItem{
public bool IsTrue { get; set; }
}
no viewmodel needed other than the one that holds MyItems
Tinister
Tinister•12mo ago
What if MyItem is part of your model, perhaps even from an assembly you don't control
Florian Voß
Florian Voß•12mo ago
MyItem is the model
Tinister
Tinister•12mo ago
Right, and say there's no IsTrue property and you can't add it either
Florian Voß
Florian Voß•12mo ago
well there must be a boolean property for you to bind it to an IsChecked property...
Tinister
Tinister•12mo ago
Hence adding a shim object (a.k.a. A view model)
Sir Rufo
Sir Rufo•12mo ago
There is no need for a boolean - use a converter and convert any property value into a boolean In my applications I have a domain layer where I declare any models the app will work with. These models are filled by services, providers or something and the models contains anything that is needed. Any mapping is done inside these services or providers.
Want results from more Discord servers?
Add your server