I've seen a lot of examples how to use Data Template Selector to show different controls according to the x:TargetType. I want to create an items control that show a RadioButton, TextBox or TextBlock according to the class tyepe. My class could be like this:
public class MyExample<T>
{
public string Name {get;set;}
public Type Type => TypeOf(T)
public T Value {get;set;}
}
I know that the Xaml can't recognize generics and I don't want to create a markup extension for supporting generics, I want to keep it simple. I don't want to create a concrete class for each type. I know that I can use a Data Trigger to set the content template according to a property (for example type name, or Type Type) but I think that should be an easier way using a Data Template Selector. Can I use the TargetType on the Type Property instead of class type?
No.
The obvious solution would be to create a sub-type and a corresponding
DataTemplatefor each type ofT. The implementation for each sub-type would be a one-liner:If you don't want to create concrete sub-types for whatever reason, you could use a
DataTemplateSelectorto select a template based on the type of eachMyExample<T>: