views:

383

answers:

2

Hi!

I want to make a reusable WPF Window suitable for different types T. I have a designer and a codebehind file.

can I do something like this?

/*  Code behind file */
public partial class MyWindows<T> : Window
{}
+2  A: 

In principle, this is certainly possible. However, not all languages support generics (equally well). XAML, for instance, doesn't support generics at all - your custom window can't be constructed from XAML nor defined in XAML therefore.

I.e., you're probably better off not using generics for something like this (or if you do, provide a bunch of predefined subclasses like MyWindowInt32 : MyWindow<int> etc. for use with XAML).

Based on a comment on another question, it sounds like you don't really need generics, it might suffice to just pass the Type as an object - this is less robust in the sense that the compiler can't check for type-errors statically, but it'll play nice with XAML - and if T is unconstrained, you're not losing much in the way of ease-of-programming either (intellisense is gone either way).

Eamon Nerbonne
You are wrong when you say "XAML, for instance, doesn't support generics at all" it does via x:TypeArguments, see http://blogs.windowsclient.net/rob_relyea/archive/2009/06/01/xaml-using-generic-types-in-xaml-2009.aspx
panamack
+1  A: 

You're doing it wrong.

What you should be doing is binding your model to your window (i.e., setting it as the window's DataContext which takes any object) and then use DataTemplates to control how your window displays data for each of your search result types.

Will
http://www.breakitdownblog.com/wp-content/uploads/2008/02/doing_it_wrong_bike.jpg
Nippysaurus
Oh yeah the MVVM model.. Is that what you mean?
PaN1C_Showt1Me
yep.
Will