Reputation: 229
I have a Xamarin ListView
in a Xamarin.Form
application. It is initialised as below:
ListView listView = new ListView
{
HasUnevenRows = true,
ItemTemplate = new DataTemplate(typeof(StudentCell)),
ItemsSource = register.StudentList,
SeparatorColor = Color.FromHex("#ddd")
};
listView.ItemSelected += OnSelection;
I can't seem to find anything online about how/ if I can make the ItemsSource
two seperate classes. I have a student list that contains data such as Name and Status, but need to load an image from another class for each item in the student list.
Is there any way to do this?
Alternatively, I have made a custom cell type, StudentCell
. Is there a way to specify a binding to another class (not a student) from within the custom template?
Upvotes: 1
Views: 1413
Reputation: 89129
This is what the VM (ViewModel) in the MVVM pattern is for. It allows your View (ListView) to display data from more than one model (Student and Image) by creating a ViewModel class that combines data from multiple sources.
In your case, you might create a StudentViewModel class that contains both a Student and an Image. Then you would bind your ListView to the ViewModel class.
Upvotes: 2
Reputation: 4746
One possibility to consider is that from the register.StudentList
, lets say its of type Student
, should you not be able to include a StudentImage
property in that class, then perhaps derive a new class, say StudentWithImage
that inherits from Student
so it will have the same properties.
You can then add your StudentImage
property to this new class that inherits from Student
.
On the result set returned, you can then enumerate all the instances of it, and set the StudentImage
property to the appropriate value by performing a lookup on your other class.
You can then just bind your list of StudentWithImage
s to your Xamarin.Forms
List
and work from a single datasource.
Upvotes: 3