views:

43

answers:

3

I'm trying to create a Window-derived class in XAML which can take a generic argument, but I can't seem to define the generic argument in the XAML so that it generates the partial class matching my code-behind file.

What I'm trying to accomplish is a replacement for all the MessageBox calls for asking the user questions, where I can give meaningful button captions ('Save and quit'/'Quit without saving'/'Don't quit' type thing). I'd like to be able to pass the window a generic argument constrained to System.Enum defining the return value for the selected option:

<Window x:Class="EvilPenguin.MultipleChoiceQuestionBox">
    ...

public partial class MultipleChoiceQuestionBox<T> : Window where T : System.Enum
{
    public MultipleChoiceQuestionBox()
    {
        InitializeComponent();
    }

    public T SelectedOption
    {
        get;
    }
}
  • Is there a way I can make my XAML generate a partial class with the correct generic argument?
  • Am I 'doing it wrong'? Is this a bad idea for some reason, or is there an easier way?
  • Is this not possible in XAML at the moment? The x:TypeArgument attribute doesn't quite do what I want, but it suggests that at least some aspects of XAML are aware of generic arguments

Any help or hints are much appreciated

+1  A: 

I'm not a XAML expert, but a quick google search for "generics in XAML markup" lead me to this MSDN article: http://msdn.microsoft.com/en-us/library/ee956431.aspx

It seems to indicate that you can indeed use generics in XAML. See if this fits your scenario.

<my:BusinessObject x:TypeArguments="x:String,x:Int32"/>
Samuel Meacham
I appreciate the help, but in this case, x:TypeArguments would be useful if I wanted the generated class to derive from Window<T>, not to make the compiler generate a partial class for MultipleChoiceQuestionBox<T>
TheEvilPenguin
+1  A: 

Try using x:Subclass and make the subclass a generic. This allows the base class to be designed (and load the XAML) and the derived to use a generic type.

chuckj
Can you elaborate a bit? I don't seem to be able to specify generic arguments on the x:Subclass attribute either, and I'm not sure how I could use this to create a class I can reuse for different enum types
TheEvilPenguin
Sure the type specified by Subclass would have generic parameters whle the class being designed would still not have generic parameters. Something like, <Window x:Class="EvilPenguin.MultipleChoiceQuestionBoxBase" x:Sublass="EvilPenguin.MultipleChoiceQuestionBox" ...
chuckj
+1  A: 

You can't do it. Here is my answer to this similar SO question:

No, you can't declare a generic type in XAML. From http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e:

Hello, you can use generic as long as you don’t use XAML. But unfortunately, if you want to use XAML to define your control, you can’t use generic…

You can create a control in XAML that inherits from a generic type by putting a x:TypeArguments attribute on the root tag, but the control itself must be concrete.

Quartermeister