0

My WPF application has an extension which I found in answer to a question on StackOverflow which creates choices for a ComboBox from an enumeration. I don't remember which post I got it from, but as this isn't the problem, it's irrelevant.

I'm using a DLL for our product's backend in my WPF application. There is an enumeration in this DLL that I want the user to pick from using a ComboBox and the afore mentioned extension. I have the proper xmlns defined in the XAML.

The problem is that my enumeration is a subclass of another class. That is, it's defined something like this:

public class MyClass {

    . . .

    public enum MyEnum { . . . }

    . . .
}

I thought that the XAML for the ComboBox should look something like this:

<ComboBox ItemsSource="{Binding Source={cs:Enumeration {x:Type ns:MyClass.MyEnum}}}" 
          . . . />

As I type "MyClass", when I type the period to separate the parent class name from the subtype's name, Intellisense just provides the upper level class names again. If I just type the subclass name after the period anyway, the compiler gives me an error: "Type MyClass.MyEnum not found".

How do I correctly specify the name of my enumeration?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123

2 Answers2

0

Try using:

{x:Type ns:MyClass+MyEnum}

http://neilmosafi.blogspot.co.uk/2007/08/dec-06-wpfxaml-xtype-and-nested-classes.html

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • This isn't working for me. I'm using VS 2010 and I still get the "Type 'MyClass+MyEnum' not found" error. The compiler is generating the error, so I can't run the executable, either. – Tony Vitabile Nov 30 '12 at 17:24
  • @TonyVitabile: Looks like this was broken in VS2010. There's a workaround with a custom markup extension in this answer: http://stackoverflow.com/a/1220266/124386 – Richard Deeming Nov 30 '12 at 17:30
  • I tried that workaround and it failed for me, too. I keep getting the same error from the VS2010 compiler when I specify the `ns:MyClass.MyEnum` class name. – Tony Vitabile Nov 30 '12 at 19:10
  • @TonyVitabile: I think you need to combine the two; use the `Type2Extension` from that answer with the `ns:MyClass+MyEnum` syntax. – Richard Deeming Nov 30 '12 at 19:12
  • That's what I did. I added the `Type2Extension` to the code-behind for the window, then I used it and the `ns:MyClass+MyEnum` syntax in the XAML. Same error. I mistyped the previous comment – Tony Vitabile Nov 30 '12 at 21:59
  • I believe the problem is that XAML doesn't support defining nested classes. – Tony Vitabile Jul 18 '14 at 13:26
0

I decided to punt on this and use the same mechanism that I use to load a ComboBox with choices pulled from a database. That is:

  1. I created an ObservableCollection in my view model class that holds the choices
  2. I populate the ObservableCollection in the view model class' constructor with objects of a generic custom class called ItemChoice. The declaration looks like:

    public ObservableCollection<ItemChoice<MyClass.MyEnum>> EnumChoices { get; set;

  3. I bound the ComboBox's ItemsSource property to the EnumChoices collection in the XAML.

  4. In the view model's constructor, I instantiate the EnumChocies collection and load it with new ItemChoice objects, one for each choice in the enumeration.

This all works. I can move on to other problems with the code now.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123