Vishful thinking…

Generics support in XAML

Posted in C#, Silverlight by viswaug on March 31, 2009

Support for creating generic classes in XAML is still lacking. The only XAML element that lets you specify the generic type argument is only the root element of the user control. I couldn’t find any way to create an instance of a generic class in XAML yet. One had a class called “NameValue” in the project and wanted to be able to create an ObservableCollection<NameValue> in XAML. The only way I was able to do it in XAML was to create another empty class called NameValueCollection that inherited from ObservableCollection<NameValue> and then create an instance of the “NameValueCollection” in XAML instead.

public class NameValueCollection : ObservableCollection<NameValue>
{
}

I think one way generics could be supported in XAML is by creating new MarkupExtensions for that purpose.

2 Responses

Subscribe to comments with RSS.

  1. Andres Olivares said, on April 8, 2009 at 3:04 pm

    Vish,

    Actually you’re on the right track. While doing my own research on XAML generic support, I ran into with the description of a MarkUpExtension implementation for limited generic support in XAML.

    Enjoy.

  2. Andres Olivares said, on April 8, 2009 at 3:15 pm

    Vish,

    Actually you’re on the right track. While doing my own research on XAML generic support, I ran into this post with the description of a MarkUpExtension implementation for limited generic support in XAML.

    Enjoy.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: