简体   繁体   中英

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 NEVER (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.

You are always returning first enum value from converter ie 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;

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();           
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM