绑定使用C#中的枚举使用本地化值下拉

时间:2014-03-07 05:50:20

标签: c# .net enums dropdownbox

绑定使用C#中的枚举使用本地化值下拉。

我对所有语言都有价值。所以我不需要从其他任何地方获取它。 我可以用不同的语言resx文件写出所有的值。但我不确定它是如何运作的。

我使用的是C#窗体。框架3.5

2 个答案:

答案 0 :(得分:2)

我使用了以下代码,它对我来说很好用

using System.Globalization;
using System.ComponentModel;
using System.Threading;

.......

.......

.......

Thread.CurrentThread.CurrentUICulture = new CultureInfo(DDLLang.SelectedValue);
ComponentResourceManager resource = new ComponentResourceManager(typeof(Default));
resource.ApplyResources(LblName, "LblName");
LblName.ID = "LblName";
Response.Write(resource.GetString("strMsg",CultureInfo.CurrentUICulture)??resource.GetString("strMsg"));

在上面的代码中,LblName是一个Label,StrMsg是一个简单的字符串。

在资源文件中,我将这样写:

名称值


Lbl.Name labelname ................

StrMsg anycustom msg ............

答案 1 :(得分:1)

private static ResourceManager _resources = new ResourceManager("MyClass.myResources",
    System.Reflection.Assembly.GetExecutingAssembly());

public static IEnumerable<string> GetLocalizedNames(this IEnumerable enumValues)
{
    foreach(var e in enumValues)
    {
        string localizedDescription = _resources.GetString(String.Format("{0}.{1}", e.GetType(), e));
        if(String.IsNullOrEmpty(localizedDescription))
        {
            yield return e.ToString();
        }
        else
        {
            yield return localizedDescription;
        }
    }
}
相关问题