wpf 使用 MVVM 显示枚举相等字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14104088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:50:38  来源:igfitidea点击:

Display enum equalivent string values using MVVM

c#wpfdata-bindingmvvmenums

提问by idish

My goal is to databind strings that equalivent to my enum.

我的目标是对等于我的枚举的字符串进行数据绑定。

    public enum Language
    {
        Unknown=0,CSharp=1,VB=2,VisualCpp=3,FSharp=4
    }

    public enum ProjectType
    {
        Unknown=0,ConsoleApplication=1,ClassLibrary=2
    }

Here's my Model:

这是我的模型:

  class PLanguage
  {
    public Language EnumLanguage { get; set; }
    public string ImagePath { get; set; }
    public List<ProjectType> EnumTypes { get; set; }

  }

MyViewModel:

我的视图模型:

  class PLanguageViewModel : ViewModelBase
  {

    public PLanguage PLanguage { get; set; }
    private ObservableCollection<string> _typeCollection;
    public PLanguageViewModel(PLanguage pLanguage)
    {
        PLanguage = pLanguage;
    }
            public ObservableCollection<string> TypeCollection
    {
        get{} //CAST PLanguage.EnumTypes FROM ENUM TO STRING
    }
    public string ImagePath
    {
        get { return PLanguage.ImagePath; }
        set
        {
            if (PLanguage.ImagePath != value)
            {
                PLanguage.ImagePath = value;
                RaisePropertyChanged(() => ImagePath);
            }
        }
    }
    public static String ConvertToString(Enum eEnum)
    {
        return Enum.GetName(eEnum.GetType(), eEnum);
    }

  }

As you can see, I have a list of enum of type ProjectType. I want to convert these to an observable collection of strings that equalivent to the enum values, so I can databind them easily in my View. I need to create a Dependency Property of that collection, how can I do that?

如您所见,我有一个 ProjectType 类型的枚举列表。我想将这些转换为与枚举值相等的可观察字符串集合,这样我就可以在我的视图中轻松地对它们进行数据绑定。我需要创建该集合的依赖属性,我该怎么做?

回答by Tilak

You can use Enum.GetNamesto get all values and ObservableCollection constructor overloadfor data Binding.

您可以使用Enum.GetNames获取数据绑定的所有值和ObservableCollection 构造函数重载

public PLanguageViewModel(PLanguage pLanguage)
{
    PLanguage = pLanguage;
    _typeCollection = new ObservableCollection<string>(Enum.GetNames(typeof(ProjectType)));
   ...
}

You can use Enum.Parseto retrieve the ProjectTypefrom observable collection.

您可以使用Enum.ParseProjectType从 observable 集合中检索。

EDITas per comment Use following to bind String to SelectItem. Now You can use DataBinding over SelectedItemin the view. You can also achieve the same using IValueConverter

根据评论编辑使用以下将字符串绑定到SelectItem. 现在您可以SelectedItem在视图中使用 DataBinding 。您也可以使用IValueConverter实现相同的目的

private ProjectType _selectedItem'

public string SelectedItem
{
   get
    {
      return ConvertEnumToString(_selectedItem);
    }
    set
    {
      _selectedItem = ConvertStringToEnum(value);
    }
}

public static string ConvertEnumToString(Enum eEnum)
{
    return Enum.GetName(eEnum.GetType(), eEnum);
}

public static ProjectType ConvertStringToEnum(string value)
{
    return (ProjectType)Enum.Parse(typeof(ProjectType), value);
}