Can I specify a generic type in XAML (pre .NET 4 Framework)?
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.
How to specify generic type argument in XAML
You can do it since .NET 4 Framework
and XAML 2009.
See Generics in XAML on MSDN
For instance:
<my:BusinessObject x:TypeArguments="x:String,x:Int32"/>
For .NET 3.5:
For XAML 2006 usage when specifically targeting WPF, x:Class must also
be provided on the same element as x:TypeArguments, and that element
must be the root element in a XAML document. The root element must map
to a generic type with at least one type argument. An example is
PageFunction.Possible workarounds to support generic usages include defining a
custom markup extension that can return generic types, or providing a
wrapping class definition that derives from a generic type but
flattens the generic constraint in its own class definition.
How to reference a generic type in the DataType attribute of a DataTemplate?
No, you cannot express a generics type in XAML. You will have to create a concrete type that extends your generic one ...
public class FooLocationTreeViewModel : LocationTreeViewModel<Foo>
{
}
WPF Data Template Selector based on Generic Types
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 DataTemplate
for each type of T
. The implementation for each sub-type would be a one-liner:
public class MyExampleInt : MyExample<int> { }
public class MyExampleString : MyExample<string> { }
If you don't want to create concrete sub-types for whatever reason, you could use a DataTemplateSelector
to select a template based on the type of each MyExample<T>
:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
switch (item)
{
case MyExample<int> i:
return IntTemplate;
case MyExample<string> s:
return StringTemplate;
}
return null;
}
WPF UserControl with generic code-behind
Unfortunately XAML does not support generic code behind, thou you can walk around this.
See links below:
http://forums.silverlight.net/forums/p/29051/197576.aspx
Can I specify a generic type in XAML (pre .NET 4 Framework)?
May be generic controls will be supported natively in future versions of Visual Studuo with
XAML 2009.
How to add Type Parameters to XAML
Check out an identical SO question.
Related Topics
How to Embed an Application Manifest into an Application Using VS2008
How to Read Regex Captures in C#
How to Write One to Many Query in Dapper.Net
Converting File into Base64String and Back Again
Catch All Unhandled Exceptions in ASP.NET Web API
Method Overloading VS Optional Parameter in C# 4.0
Calculated Column in Ef Code First
Winforms Global Exception Handling
How to Capture the Mouse Move Event
C# Windows Forms Application - Updating Gui from Another Thread and Class
How to Get the Line Number Which Threw Exception
How to Keep a .Net Console App Running
How to Calculate the Ip Range When the Ip Address and the Netmask Is Given
App.Config for a Class Library
How to Receive Plug & Play Device Notifications Without a Windows Form