Matt Hamilton Matt Hamilton - 5 months ago 41
C# Question

Can I specify a generic type in XAML (pre .NET 4 Framework)?

In XAML I can declare a DataTemplate so that the template is used whenever a specific type is displayed. For example, this DataTemplate will use a TextBlock to display the name of a customer:

<DataTemplate DataType="{x:Type my:Customer}">
<TextBlock Text="{Binding Name}" />

I'm wondering if it's possible to define a DataTemplate that will be used any time an IList<Customer> is displayed. So if a ContentControl's Content is, say, an ObservableCollection<Customer> it would use that template.

Is it possible to declare a generic type like IList in XAML using the {x:Type} Markup Extension?


Not out of the box, no; but there are enterprising developers out there who have done so.

Mike Hillberg at Microsoft played with it in this post, for example. Google has others of course.