Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF custom control with generics - possible?

I'd like to create a custom WPF control using generics:

public class MyGenericTypeControl<T> : ItemsControl 
{   
   // ...
}

Is this possible to do? In my initial experimentation, I get design-time/compile-time XAML errors as soon as I try to add this control somewhere. This isn't surprising, as construction of my custom control requires additional information that XAML does not provide.

Any ideas?

like image 227
kindohm Avatar asked Aug 11 '11 14:08

kindohm


1 Answers

There is limited support using x:TypeArguments

For XAML 2006 usage, and XAML that is used for WPF applications, the following restrictions exist for x:TypeArguments and generic type usages from XAML in general:

  • Only the root element of a XAML file can support a generic XAML usage that references a generic type.
  • The root element must map to a generic type with at least one type argument. An example is PageFunction<T>. The page functions are the primary scenario for XAML generic usage support in WPF.
  • The root element XAML object element for the generic must also declare a partial class using x:Class. This is true even if defining a WPF build action.
  • x:TypeArguments cannot reference nested generic constraints.
like image 138
H.B. Avatar answered Sep 27 '22 18:09

H.B.