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;
}
}
Any help or hints are much appreciated
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With