I have a ViewModel defined like this:
public class LocationTreeViewModel :
ObservableCollection, INotifyPropertyChanged
I know, that I'm a little late to the party, but I want post an answer for all those who may see this question in the future:
It is possible.
You can see the whole code in the answer to this question: DataTemplates and Generics. But since it is quite long, I will just copy the important bits. If you want more details, then look into the referenced question.
You need to write a MarkupExtension
which can provide a closed generic type.
public class GenericType : MarkupExtension
{
public GenericType() { }
public GenericType(Type baseType, params Type[] innerTypes)
{
BaseType = baseType;
InnerTypes = innerTypes;
}
public Type BaseType { get; set; }
public Type[] InnerTypes { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
Type result = BaseType.MakeGenericType(InnerTypes);
return result;
}
}
Now you can define your type which closes your generic type in xaml, and then use the closed generic type as DataType
of an DataTemplate
.
Be happy that the defined DataTemplate
gets automatically selected by WPF.