MVVM framework is a great way or you can say it is the correct way to develop Xaml application, that's including WPF, Windows Phone, Windows 8 and our good old friend Silverlight application.
Sometime you have a collection of object that has different Type and you want to bind it to ListBox control, and also want it to render different UI base on object type for each ListBox's items.
In this example I have a collection of products, consist of two types: Book and Mobile Phone. I want to display them in a ListBox.
You can take a look at a completed sample WPF project in GitHub.
Sometime you have a collection of object that has different Type and you want to bind it to ListBox control, and also want it to render different UI base on object type for each ListBox's items.
In this example I have a collection of products, consist of two types: Book and Mobile Phone. I want to display them in a ListBox.
You can take a look at a completed sample WPF project in GitHub.
In MainViewModel class, I have a collection of BookModel and MobilePhoneModel object that I want to display in a ListBox.
Here is BookModel class file.
MobilePhoneModel class file.
Here is the XAML file, where I bind ItemSource property to the products collection. Then I create DataTemplate with a ContentControl, and then bind its Content using ViewProductConverter. The converter responsible in deciding what UI will be displayed for each correspondent object model.
ViewProductConverter class file.