I have a service returning an array of type Party. Party has two subtypes, Person and Organization. I’m consuming this service in my WPF application (Prism, MVVM) from a view model. In the constructor of this view model I populate an observable collection of type Party:
public PhoneBookViewModel(IPhoneBookService phoneBookProxy)
{
_phoneBookProxy = phoneBookProxy;
var parties = _phoneBookProxy.GetAllParties();
_parties = new ObservableCollection<Party>(parties.ToList());
}
So far so good. In my PhoneBookView I have an ItemsControl that binds to this collection. In this control I want to render each Party by using another View (and its view model). So when Party is of type Person, inject PersonView and pass the Party object to the constructor of the PersonViewModel, and when Party is of type Organization, render OrganizationView, and so on… You get the picture (or?).
But I can’t figure out how to do this in XAML. Any ideas?
This is probaly not the best way of doing it, so if you can recommend a better approach, please enlighten me 🙂
Thanks!
Lets examine this from the view towards the model:
Lets assume we have 2 different types of views, 1 type of view model:
If both views are binded to the same view model, you would end up with the same view.
Lets try again with 2 different types of views and 2 different types of view models:
This IS possible.
Now if you model your view-models and models like this (pseudo code):
then you will get correct type of VMs for each model item,
view will be binded to VM items, not model items.
View’s datatemplates:
View’s itemscontrol: