I have a class of MyClass
and want to set it as the DataType for a HierarchicalDataTemplate.
What is the syntax for this in XAML? (I kno
itowlson's approach is a good one but it is just a start. Here's something that will work for your case (and most, if not all, cases):
public class GenericType : MarkupExtension
{
public Type BaseType { get; set; }
public Type[] InnerTypes { get; set; }
public GenericType() { }
public GenericType(Type baseType, params Type[] innerTypes)
{
BaseType = baseType;
InnerTypes = innerTypes;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
Type result = BaseType.MakeGenericType(InnerTypes);
return result;
}
}
Then, you are able to create any type with any level of depth in your XAML. For example:
There's a few key ideas here: