Binding ComboBoxes to enums... in Silverlight!

Posted by Domenic on Stack Overflow See other posts from Stack Overflow or by Domenic
Published on 2009-08-15T08:58:42Z Indexed on 2010/05/10 18:54 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

So, the web, and StackOverflow, have plenty of nice answers for how to bind a combobox to an enum property in WPF. But Silverlight is missing all of the features that make this possible :(. For example:

  1. You can't use a generic EnumDisplayer-style IValueConverter that accepts a type parameter, since Silverlight doesn't support x:Type.
  2. You can't use ObjectDataProvider, like in this approach, since it doesn't exist in Silverlight.
  3. You can't use a custom markup extension like in the comments on the link from #2, since markup extensions don't exist in Silverlight.
  4. You can't do a version of #1 using generics instead of Type properties of the object, since generics aren't supported in XAML (and the hacks to make them work all depend on markup extensions, not supported in Silverlight).

Massive fail!

As I see it, the only way to make this work is to either

  1. Cheat and bind to a string property in my ViewModel, whose setter/getter does the conversion, loading values into the ComboBox using code-behind in the View.
  2. Make a custom IValueConverter for every enum I want to bind to.

Are there any alternatives that are more generic, i.e. don't involve writing the same code over and over for every enum I want? I suppose I could do solution #2 using a generic class accepting the enum as a type parameter, and then create new classes for every enum I want that are simply

class MyEnumConverter : GenericEnumConverter<MyEnum> {}

What are your thoughts, guys?

© Stack Overflow or respective owner

Related posts about databinding

Related posts about enums