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 8545983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:54:53+00:00 2026-06-11T12:54:53+00:00

I have an Enum that needs to be shown in ComboBox . I have

  • 0

I have an Enum that needs to be shown in ComboBox. I have managed to get enum values to combobox using ItemsSource and I’m trying to localize them. I thought that that could be done using value converter but as my enum values are already strings compiler throws error that IValueConverter can’t take string as input. I’m not aware of any other way to convert them to other string value. Is there some other way to do that (not the localization but conversion)?

I’m using this marku extension to get enum values

[MarkupExtensionReturnType(typeof (IEnumerable))]
public class EnumValuesExtension : MarkupExtension {
    public EnumValuesExtension() {}

    public EnumValuesExtension(Type enumType) {
        this.EnumType = enumType;
    }

    [ConstructorArgument("enumType")]
    public Type EnumType { get; set; }
    public override object ProvideValue(IServiceProvider serviceProvider) {
        if (this.EnumType == null)
            throw new ArgumentException("The enum type is not set");
        return Enum.GetValues(this.EnumType);
    }
}

and in Window.xaml

<Converters:UserTypesToStringConverter x:Key="userTypeToStringConverter" />
....
<ComboBox ItemsSource="{Helpers:EnumValuesExtension Data:UserTypes}" 
            Margin="2" Grid.Row="0" Grid.Column="1" SelectedIndex="0" TabIndex="1" IsTabStop="False">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="{x:Type Data:UserTypes}">
            <Label Content="{Binding Converter=userTypeToStringConverter}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And here is converter class, it’s just a test class, no localization yet.

public class UserTypesToStringConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        return (int) ((Data.UserTypes) value) == 0 ? "Fizička osoba" : "Pravna osoba";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
         return default(Data.UserTypes);
     }
}

— EDIT —

Enum is generated by ADO.NET Diagram and can’t be changed.

  • 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-06-11T12:54:54+00:00Added an answer on June 11, 2026 at 12:54 pm

    Yes, by the time you pass the value into the converter it will be a string as the default type converter for Enum (EnumConverter) for GetStandardValues (i.e. Enum.GetValues()) returns an enumerable of the fields as strings.

    The best way to solve this to write a custom type converter to decorate your Enums with. Fortunately you are not the first person that has needed to this, see below for code sample.

    public class EnumTypeConverter : EnumConverter
    {
        public EnumTypeConverter()
            : base(typeof(Enum))
        {
        }
    
        public EnumTypeConverter(Type type)
            : base(type)
        {
        }
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || TypeDescriptor.GetConverter(typeof(Enum)).CanConvertFrom(context, sourceType);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
                return GetEnumValue(EnumType, (string)value);
    
            if (value is Enum)
                return GetEnumDescription((Enum)value);
    
            return base.ConvertFrom(context, culture, value);
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is Enum && destinationType == typeof(string))
                return GetEnumDescription((Enum)value);
    
            if (value is string && destinationType == typeof(string))
                return GetEnumDescription(EnumType, (string)value);
    
            return base.ConvertTo(context, culture, value, destinationType);
        }
    
        public static bool GetIsEnumBrowsable(Enum value)
        {
            var fieldInfo = value.GetType().GetField(value.ToString());
            var attributes = (BrowsableAttribute[])fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);
    
            return !(attributes.Length > 0) || attributes[0].Browsable;
        }
    
        public static string GetEnumDescription(Enum value)
        {
            var fieldInfo = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }
    
        public static string GetEnumDescription(Type value, string name)
        {
            var fieldInfo = value.GetField(name);
            var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : name;
        }
    
        public static object GetEnumValue(Type value, string description)
        {
            var fields = value.GetFields();
            foreach (var fieldInfo in fields)
            {
                var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
                if (attributes.Length > 0 && attributes[0].Description == description)
                    return fieldInfo.GetValue(fieldInfo.Name);
    
                if (fieldInfo.Name == description)
                    return fieldInfo.GetValue(fieldInfo.Name);
            }
    
            return description;
        }
    
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return base.GetStandardValues(context);
        }
    
    }
    

    Usage

    [TypeConverter(typeof(EnumTypeConverter))]
    public enum UserTypes : int
    {
        [Browsable(false)]
        Unkown,    
        [Description("Local")]
        LocalUser,
        [Description("Network")]
        NetworkUser,
        [Description("Restricted")]
        RestrictedUser
    } 
    

    As you can see, the above enum we have used the Description attribute to decorate each field with a user friend description and have overridden the type converter to first look for this attribute.

    Not 100% but to get this to work with your code, you will also need to change your MarkupExtension to be the following (Note: I have not tested this, so some work on your part is required).

    [MarkupExtensionReturnType(typeof (IEnumerable))]
    public class EnumValuesExtension : MarkupExtension {
    
        public EnumValuesExtension() {}
    
        public EnumValuesExtension(Type enumType) 
        {
            this.EnumType = enumType;
        }
    
        [ConstructorArgument("enumType")]
        public Type EnumType { get; set; }
    
        public override object ProvideValue(IServiceProvider serviceProvider) 
        {
            if (this.EnumType == null)
                throw new ArgumentException("The enum type is not set");
    
            var converter = TypeDescriptor.GetConverter(this.EnumType);
            if (converter != null && converter.GetStandardValuesSupported(this.EnumType))        
                return converter.GetStandardValues(this.EnumType);
    
            return Enum.GetValues(this.EnumType);
        }
    }
    

    Also, I have only done limited localisation for an application however I believe this is the best and most maintainable approach as will be able to leverage the existing .NET localisation tools (e.g. satellite assemblies)

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

Sidebar

Related Questions

I have a combobox that is bound to an enum using the following code:
I have a generic class that needs to limit an enum depending on the
I have many tables that use Lookup/Enum references for most of their column values.
I have an enum called SiteTypes that contains several values that are all bound
I have 2 variables of type object that needs to be compared using a
i have a enum like that : public enum Mode { [*I need a
If I have an enum that's marked with [Flags] , is there a way
I have a java enum that is used in my web application. I also
Assuming that I have this: enum { A = 0x2E, B = 0x23, C
I have 3 similar tables, that would all have the same enum type field.

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.