C
C#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?
SR
Sir Rufo160d ago
Yes of course
T
Tinister160d 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)
FV
Florian Voß158d 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
C
Cin158d ago
I will admit my example was a simple one, just for proof of concept
T
Tinister158d 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?
FV
Florian Voß158d 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
T
Tinister158d ago
What if MyItem is part of your model, perhaps even from an assembly you don't control
FV
Florian Voß158d ago
MyItem is the model
T
Tinister158d ago
Right, and say there's no IsTrue property and you can't add it either
FV
Florian Voß158d ago
well there must be a boolean property for you to bind it to an IsChecked property...
T
Tinister158d ago
Hence adding a shim object (a.k.a. A view model)
SR
Sir Rufo158d 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
More Posts
2 QuestionsHi, I need to ask 2 questions as i am new to C#. So im missing 2 tasks to deploy my first app. And iRaw SQL Query QuestionI have a raw SQL query that I need to execute against a database that's external to my application. ✅ Help for unityGood evening I am looking for someone who can teach me how to create a game from A to Z. Even if I dAny way to get XAML Auto-complete in VS Code?I recognize this isn't quite a C# question, but it's related. I want to make cross platform apps usi.Net 8 Blazor Web App Identity Framework HelpSuper quick overview: I have a Blazor Web App that is single process only hosting UI. It communicaRegex Group Containshey simple easy question, but are there ways to access individual capture groups in a regex match? I"netsh" cmd Commands not being executedWell, I am trying to set up some firewall-rules and such, the problem is non of the commands is beinAutomate the process of transferring a Microsoft SQL Server .bak file to MongoDBI'm looking to automate the process of transferring a Microsoft SQL Server backup file (e.g., bulk.bMapping controller to / without breaking other controllersHi there, I would like to rename `HomeController` to `DefaultController` and instead of going to `/helpwhy is doesn't workTrack Thread/Task Progress through SignalR/WebSocketsI have an ASP.NET Web Core API and an Angular project both connected. In the dashboard of my Angularazor pages vs .cshtmlTeam , What is the difference between razor pages and cshtml.? razor pages looks like old asp.net w✅ mobile applicationMy question is quite fundamental, the way I imagine mobile apps is that they utilize an API to conneIt's not showing the database in datagridviewI'm having trouble trying to show the database that the teacher gave me and the demo she gave me to Can't seem to make a universal filepathHi there could anyone explain how I can make a universal filepath so other pc's can locate the fileshey! looking for fellow C# noobs!Does anyone want to vc and group study,go over things etc? No dumb questions or rudeness I'm alsl beStuttering in UI and I don't know whyEverytime I launch the app, the very first time I navigate to any FlyoutItem I get this stuttering iIssue with Filtering in DataGridI use Telerik UI's DataGrid control for .Net Maui. As you see VIPStatus column holds values such as Minimum Required Knowledge before I am job ready.Hello guys, I want to know this info from someone that has work experience. What is minimum requiredAsssembly Version WrongIt was working for a little bit, but now it's displaying `v1.0.0-alpha+9fb67d6af368e6cd5a05c8f401327