Basically, you need to create data templates programmatically ... It's not very simple, but I think you can achieve this with a class FrameworkElementFactory:
public void AddDataTemplateForView(Type viewType)
{
string viewModelTypeName = viewType.FullName + "Model";
Type viewModelType = Assembly.GetExecutingAssembly().GetType(viewModelTypeName);
DataTemplate template = new DataTemplate
{
DataType = viewModelType,
VisualTree = new FrameworkElementFactory(viewType)
};
this.Resources.Add(viewModelType, template);
}
I have not tested it, so several settings may be required ... For example, I'm not sure what type of resource key should be, since it is usually set implicitly when you install DataType in XAML
source
share