Using common arguments in a WPF window defined in XAML

I'm trying to create a Window-derived class in XAML that can take a general argument, but I cannot describe a general argument in XAML so that it generates an incomplete class that matches my code file.

What I'm trying to do is replace all MessageBox calls to ask for custom questions, where I can give meaningful button headers ("Save and exit" / "Exit without saving" / "Do not leave", type item). I would like to be able to pass to the window a general argument, limited to System.Enum, which defines the return value for the selected parameter:

<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 to get my XAML to generate a partial class with the correct general argument?
  • Am I doing it wrong? Is it a bad idea for some reason, or is there an easier way?
  • Is this currently not possible in XAML? The x: TypeArgument attribute doesn't quite do what I want, but it assumes at least some aspects of XAML are aware of common arguments

Any help or tips are greatly appreciated.

+6
generics c # wpf xaml
source share
3 answers

You cannot do this. Here is my answer to this similar SO question :

No, you cannot 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 the generic option as long as you are not using XAML. But unfortunately, if you want to use XAML to define your control, you cannot use the generic ...

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

+6
source share

I'm not a XAML expert, but a quick Google search of โ€œgenerics in XAML markupโ€ leads me to this MSDN article: http://msdn.microsoft.com/en-us/library/ee956431.aspx

It seems you can really use generics in XAML. See if this fits your scenario.

 <my:BusinessObject x:TypeArguments="x:String,x:Int32"/> 
+3
source share

Try using x:Subclass and make the subclass generic. This allows you to create a base class (and load XAML), and the resulting one - use a common type.

+2
source share

All Articles