3

Possible Duplicate:
Is it Possible to Make a Generic Control in .Net 3.5?

How do I create a UserControl<T> in C# in Winforms or Webforms?

public partial class MyView<T> : UserControl
 { 
        public MyView()
        {
            InitializeComponent();  
        } 
 }

When I try this, I get the following error message:

The name 'InitializeComponent' does not exist in the current context

Is it not possible to make a UserControl generic in .NET 3.5?

Community
  • 1
  • 1
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 1
    Is this a WinForms user control? The form designer can't handle generic forms or user controls; it will give you an error when you try to open `MyView`. – Tim Robinson Nov 29 '10 at 12:45
  • its a WPF UserControl! sorry forgot about that. – Elisabeth Nov 29 '10 at 12:59
  • You just added all the tags except for WPF? What does asp.net and winforms has to do with it? What were you thinking George? – Arcturus Nov 29 '10 at 13:06
  • @Arcturus I made the edit at 12:56UTC, she responded in a comment at 12:59 UTC that it was WPF. The reason I didn't put the WPF tag is because at the time she hadn't speficially said it was WPF, and from the code it could have been Winforms or Webforms development. That's "What I was thinking." – George Stocker Nov 30 '10 at 21:55
  • @George Yes, but it was neither. Did the InitializeComponent method not ring a bell? – Arcturus Dec 01 '10 at 08:20
  • @Arcturus both Winforms and Webforms utilize `InitializeComponent()`. – George Stocker Dec 01 '10 at 13:08
  • You are correct, it does indeed. But why instead of asking Lisa, did you edit the question and put both Winforms and Webforms in there? – Arcturus Dec 01 '10 at 13:24

2 Answers2

3

This is a partial class split across several files. Open the MyView.Designer.cs file and make it generic there too.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
3

The short answer is no!

The Xaml just cannot handle it in .NET 3.5. You can however derive of generic classes, so

public partial class MyView : MyView<T>

But you will need to specify it in the Xaml as well with the TypeArgument

<my:BusinessObject x:TypeArguments="x:String"/>
Arcturus
  • 26,677
  • 10
  • 92
  • 107