Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 334159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:01:42+00:00 2026-05-12T10:01:42+00:00

So, the web, and StackOverflow, have plenty of nice answers for how to bind

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:01:43+00:00Added an answer on May 12, 2026 at 10:01 am

    Agh, I spoke too soon! There is a perfectly good solution, at least in Silverlight 3. (It might only be in 3, since this thread indicates that a bug related to this stuff was fixed in Silverlight 3.)

    Basically, you need a single converter for the ItemsSource property, but it can be entirely generic without using any of the prohibited methods, as long as you pass it the name of a property whose type is MyEnum. And databinding to SelectedItem is entirely painless; no converter needed! Well, at least it is as long as you don’t want custom strings for each enum value via e.g. the DescriptionAttribute, hmm… will probably need another converter for that one; hope I can make it generic.

    Update: I made a converter and it works! I have to bind to SelectedIndex now, sadly, but it’s OK. Use these guys:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Data;
    
    namespace DomenicDenicola.Wpf
    {
        public class EnumToIntConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                // Note: as pointed out by Martin in the comments on this answer, this line
                // depends on the enum values being sequentially ordered from 0 onward,
                // since combobox indices are done that way. A more general solution would
                // probably look up where in the GetValues array our value variable
                // appears, then return that index.
                return (int)value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return Enum.Parse(targetType, value.ToString(), true);
            }
        }
        public class EnumToIEnumerableConverter : IValueConverter
        {
            private Dictionary<Type, List<object>> cache = new Dictionary<Type, List<object>>();
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var type = value.GetType();
                if (!this.cache.ContainsKey(type))
                {
                    var fields = type.GetFields().Where(field => field.IsLiteral);
                    var values = new List<object>();
                    foreach (var field in fields)
                    {
                        DescriptionAttribute[] a = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (a != null && a.Length > 0)
                        {
                            values.Add(a[0].Description);
                        }
                        else
                        {
                            values.Add(field.GetValue(value));
                        }
                    }
                    this.cache[type] = values;
                }
    
                return this.cache[type];
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    With this sort of binding XAML:

    <ComboBox x:Name="MonsterGroupRole"
              ItemsSource="{Binding MonsterGroupRole,
                                    Mode=OneTime,
                                    Converter={StaticResource EnumToIEnumerableConverter}}"
              SelectedIndex="{Binding MonsterGroupRole,
                                      Mode=TwoWay,
                                      Converter={StaticResource EnumToIntConverter}}" />
    

    And this sort of resource-declaration XAML:

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:ddwpf="clr-namespace:DomenicDenicola.Wpf">
        <Application.Resources>
            <ddwpf:EnumToIEnumerableConverter x:Key="EnumToIEnumerableConverter" />
            <ddwpf:EnumToIntConverter x:Key="EnumToIntConverter" />
        </Application.Resources>
    </Application>
    

    Any comments would be appreciated, as I’m somewhat of a XAML/Silverlight/WPF/etc. newbie. For example, will the EnumToIntConverter.ConvertBack be slow, so that I should consider using a cache?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching the web and all over stackoverflow, but yet couldn't find
i have been all over the web, stackoverflow included and just can't seem to
Let's assume that Stackoverflow offers web services where you can retrieve all the questions
I've searched the web and I've searched the questions here on stackoverflow, but I
I have a string like this a web url https://stackoverflow.com/questions/ 3260091 /databases-and-deep-copy how can
I have a long web url in my xsl variable. eg. @url = http://stackoverflow.com/questions/ask/2434/35454
I basically have the exact same problem as here: http://stackoverflow.com/questions/2416155/issue-in-executing-spring-web-project-in-eclipse-on-tomcat-server but the fix for
I have been looking over the web but have not come across an answer
I have developed web applications using JSF (myfaces components). But in these days of
Ok, I have an app as described in this post: https://stackoverflow.com/questions/1623105/good-database-structure-for-a-new-web-app I've prepared a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.