I have a service returning an array of type BaseItem. BaseItem has N subtypes. 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 BaseItem:
public CurrentViewModel(IDataService dataService)
{
_dataService = dataService
var baseItems = _dataService.GetAllItems(); // there are many kinds of BaseItems
_baseItems = new ObservableCollection<BaseItem>(baseItems.ToList());
}
So far so good. In my CurrentView I have an ItemsControl that binds to this collection. In this control I want to render each BaseItem by using another View (and its view model).
Until now, I can’t use DataTemplateSelector because I can’t define each DataTemplate in it, I’m loading N modules (which contain classes which inherit from BaseItem) and PRISM loads them dinamically from an specific folder.
I’m using view Model first approach, what other alternatives do I have to implement the scenario?
simply export your datatemplate resources as a resourcedictionary from your moduls with the specific subtype as DataType. i do this with MEF and in my main app merge this resourcedictionarys. now all datatemplates/views are known to WPF and the itemscontrol render every subtype viewmodel like you want.
EDIT:
modul1.dll
ResourceDictionary in modul1.dll exported with MEF
modul2.dll
ResourceDictionary in modul2.dll exported with MEF
your main app
app.xaml.cs
OnStartup
your itemscontrol just need the collection of BaseItemViewModels as itemssource