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
This is not supported in WPF 3.x out of the box (I think it may be in 4.0, but I'm not sure); but it's easy to set up with a markup extension.
First, you need to create a markup extension class that takes the type parameter as a constructor argument:
public class MyClassOf : MarkupExtension
{
private readonly Type _of;
public MyClassOf(Type of)
{
_of = of;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return typeof(MyClass<>).MakeGenericType(_of);
}
}
Now you use this markup extension in place of the x:Type extension:
Needless to say, this can be generalised to allow instantiation of arbitrary generic types; I haven't shown this because it adds a wee bit more complexity.