我的目标是绑定与我的枚举相等的字符串。
public enum Language
{
Unknown=0,CSharp=1,VB=2,VisualCpp=3,FSharp=4
}
public enum ProjectType
{
Unknown=0,ConsoleApplication=1,ClassLibrary=2
}
这是我的模型:
class PLanguage
{
public Language EnumLanguage { get; set; }
public string ImagePath { get; set; }
public List<ProjectType> EnumTypes { get; set; }
}
我的视图模型:
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);
}
}
如您所见,我有一个 ProjectType 类型的枚举列表。我想将这些转换为与枚举值相等的可观察字符串集合,因此我可以在我的视图中轻松地对它们进行数据绑定。我需要创建该集合的依赖属性,我该怎么做?