Debhere
Debhere

Reputation: 1075

How to convert enum to localized enum structure in WPF

I have a question here to ask. I have an enum which at runtime shows in the UI. It has three values.

enum ExpiryOptions
{
   Never,
   After,
   On
}

Now from the userControl when it loads its shows Never, After, on.

    <ComboBox x:Name="accessCombo" Margin="5" Height="25" Width="80"
        ItemsSource="{Binding Source={StaticResource ResourceKey=expiryEnum}, 
        Converter={StaticResource enumtoLocalizeConverter}}"/>

In English its fine but the problem is, if the software is used as a localized settings the same strings appear. And not any localized strings.

In the converter I have a written a code like this

        public object Convert(object value, Type targetType,
                    object parameter, CultureInfo culture)
        {
            ExpiryOption[] myEnum = value; // This myEnum is having all the enum options.

        // Now what shall I write here
        //if I write a code like this
        if(myEnum[0] == Properties.Resources.Never)
            return Properties.Resources.Never;
        else if(myEnum[1] == Properties.Resources.After)
            return Properties.Resources.After;
        else if(myEnum[2] == Properties.Resources.On)
            return Properties.Resources.On;


        }

then the enum in the UI fills with N E V E R (vertically) In English Language settings. Obviously the first string matches and fills with Never other two options are missing. Any suggestions and help is extremely needed.

Upvotes: 2

Views: 680

Answers (3)

Blablablaster
Blablablaster

Reputation: 3348

Assuming you have defined resources strings for Never, After, On as strings in class Properties as ExpiryOptionsNever, ExpiryOptionsAfter, ExpiryOptionsOn respectively(of course with strings you need) I would write this converter:

public class EnumConverter: IValueConverter{
    public Dictionary<ExpiryOptions, string> localizedValues = new Dictionary<ExpiryOptions, string>();

    public EnumConverter(){
        foreach(ExpiryOptionsvalue in Enum.GetValues(typeof(ExpiryOptions)))
        {
             var localizedResources = typeof(Resources).GetProperties(BindingFlags.Static).Where(p=>p.Name.StartsWith("ExpiryOptions"));
             string localizedString = localizedResources.Single(p=>p.Name="ExpiryOptions"+value).GetValue(null, null) as string;
             localizedValues.Add(value, localizedString);
        }
    }
    public void Convert(...){
        return localizedValues[(ExpiryOptions)value];
    }
}

This is essentially what user Blam suggested in the comments

Upvotes: 0

Rohit Vats
Rohit Vats

Reputation: 81253

You are always returning first enum value from converter i.e. string value Never which is char array hence you are seeing one item as single char in your comboBox.

Instead you should return string list:

List<string> descriptions = new List<string>();
foreach(ExpiryOption option in myEnum)
{
   if(option == Properties.Resources.Never)
       descriptions.Add(Properties.Resources.Never);
   else if(option == Properties.Resources.After)
       descriptions.Add(Properties.Resources.After);
   else if(option == Properties.Resources.On)
       descriptions.Add(Properties.Resources.On);
}
return descriptions;

Upvotes: 1

Harrison
Harrison

Reputation: 3963

You need to get the value passed into the ValueConverter to use it as follows.

[ValueConversion(typeof(ExpiryOptions), typeof(string))]
public class MyEnumConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ExpiryOptions option= 
            (ExpiryOptions)Enum.Parse(typeof(ExpiryOptions),value.ToString());

        // Now that you have the value of the option you can use the culture info 
        // to change the value as you wish and return the changed value.
        return option.ToString();           
    }
}

Upvotes: 0

Related Questions