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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With