Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 枚举到方法下拉框_C#_Syntax_Enums_Combobox - Fatal编程技术网

C# 枚举到方法下拉框

C# 枚举到方法下拉框,c#,syntax,enums,combobox,C#,Syntax,Enums,Combobox,基本上,这样做的最佳方式是什么 我有一个枚举,其中包含所有可能的分辨率,我希望它们显示在下拉组合框中 到目前为止,我发现我可以将枚举绑定到组合框,如下所示: comboBox2.DataSource = Enum.GetNames(typeof(Resolution)); 然而,在一种方法中,我有: public void testmethod(Resolution res){} 我想不出一个办法来改变。我正在考虑将方法更改为使用字符串,但随后我必须在方法中执行case或ifs以转换回枚举

基本上,这样做的最佳方式是什么

我有一个枚举,其中包含所有可能的分辨率,我希望它们显示在下拉组合框中

到目前为止,我发现我可以将枚举绑定到组合框,如下所示:

comboBox2.DataSource = Enum.GetNames(typeof(Resolution));
然而,在一种方法中,我有:

public void testmethod(Resolution res){}
我想不出一个办法来改变。我正在考虑将方法更改为使用字符串,但随后我必须在方法中执行
case
if
s以转换回枚举

此外,我理想情况下希望某些名称具有空格。我读过关于
[Description(“Description with spaces”)]
的文章,但我认为这只适用于ToString

即使我执行某种循环并通过ToString将每个项添加到框中,它仍然会返回一个字符串

我真的不知道如何继续,除了一起转储Enum,然后采用另一种方法


我只是想知道在类似的情况下,您会怎么做?

您可以使用
Enum.Parse(Type t,string s)
方法从字符串中获取枚举,在您的情况下,它将是:

Resolution r = (Resolution)Enum.Parse(typeof(Resolution), input);
关于您的描述想法,我在代码中使用以下内容:

public static class EnumExtender
{
    public static string StringValue(this Enum value)
    {
        FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
        EnumStringValueAttribute[] attributes = (EnumStringValueAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumStringValueAttribute), false);


        if (attributes.Length > 0)
        {
            return attributes[0].Value;
        }


        return value.ToString();
    } 
}

public class EnumStringValueAttribute : Attribute
{
    public string Value { get; set; }


    public EnumStringValueAttribute(string value) : base()
    {
        this.Value = value;
    }
}

当然,您必须记住使用扩展方法来获取描述-但是将其转换回是不同的。

我将使用
查找编辑
,并将枚举值绑定到键和
Enum.GetNames(typeof(Resolutions))设置为编辑”对话框上显示的值。然后,当用户选择一个项目时,您将获得实际值而不是名称。

您可以使用:

分辨率;
if(枚举TryParse(输入,输出分辨率))
{
//使用res
}
其他的
{
//输入不是有效的分辨率值
}

难道你不能只做一个
枚举解析(typeof(Resolution),comboBox2.SelectedText)

因此,对
testmethod
的调用如下所示:

testmethod((Resolution)Enum.Parse(typeof(Resolution), comboBox2.SelectedText));

假设组合框的
DropDownStyle
设置为DropDownList

我会使用某种映射-每个枚举值都有自己的字符串描述

这方面的代码可以是:

public enum Resolution
{
   High,
   Medium,
   Low
}

Dictionary<Resolution, string> Descriptions = new Dictionary<Resolution, string>();
Descriptions.Add(Resolution.High, "1920x1080");
Descriptions.Add(Resolution.Medium, "1280x720");
Descriptions.Add(Resolution.Low, "800x600");

comboBox2.DataSource = Descriptions.Values;

public void testmethod(Resolution res)
{
   string description = Descriptions[res];
   ...
}

public void testmethod2(string description)
{
   Resolution res = Descriptions.Keys.ToList().Find(k => Descriptions[k].Equals(description));
   ...
}
公共枚举解析
{
高,,
中等,
低
}
字典描述=新字典();
说明。添加(分辨率高,“1920x1080”);
说明.添加(分辨率.Medium,“1280x720”);
说明。添加(分辨率低,“800x600”);
comboBox2.DataSource=Descriptions.Values;
公共无效测试方法(分辨率)
{
字符串描述=描述[res];
...
}
公共void testmethod2(字符串描述)
{
Resolution res=Descriptions.Keys.ToList().Find(k=>Descriptions[k].Equals(description));
...
}

填充要绑定到的可观察集合

public class MyVM : BindableObject //where BindableObject is a base object that supports INotifyPropertyChanged and provides a RaisePropertyChanged method
{
    private readonly ObservableCollection<MyEnum> _myEnums;
    private MyEnum _selectedEnum;

    public MyVM()
    {
       //create and populate collection
       _myEnums = new ObservableCollection<MyEnum>();
       foreach (MyEnum myEnum in Enum.GetValues(typeof(MyEnum)))
       {
         _myEnums.Add(myEnum);
       }
     }

     //list property to bind to
     public IEnumerable<MyEnum> MyEnumValues
     {
         get { return _myEnums; }
     }

     //a property to bind the selected item to
     public MyEnum SelectedEnum
     {
         get { return __selectedEnum; }
         set
         {
            if (!Equals(__selectedEnum, value))
            {
              __selectedEnum = value;
              RaisePropertyChanged("SelectedEnum");
            }
         }
     }
}
公共类MyVM:BindableObject//其中BindableObject是一个基本对象,支持INotifyPropertyChanged并提供RaisePropertyChanged方法
{
私有只读可观察集合;
私有髓鞘;
公共MyVM()
{
//创建并填充集合
_髓鞘=新的可观察集合();
foreach(Enum.GetValues中的MyEnum MyEnum(typeof(MyEnum)))
{
_添加(myEnum);
}
}
//要绑定到的列表属性
公共IEnumerable MyEnumV值
{
获取{return\u myEnums;}
}
//要将选定项绑定到的属性
公共选择性脊髓
{
获取{return\uuu selectedEnum;}
设置
{
如果(!等于(uuu selectedEnum,value))
{
__selectedEnum=值;
RaisePropertyChanged(“SelectedEnum”);
}
}
}
}
然后在xaml中绑定:

<ComboBox ItemsSource="{Binding Path=MyEnumValues}"
          SelectedItem="{Binding Path=SelectedEnum}"/>


请注意,从技术上讲,由于列表在运行时没有变化,我们不需要
observateCollection
a
list
就可以了,但我认为
observateCollection
是使用VM时养成的一个好习惯。

我一直收到一条红色下划线无法将类型“object”隐式转换为“resolution”。显式转换退出(是否缺少转换?)。。。有什么想法,我需要什么,因为我开始走出我的深度?啊。。对不起,我弄错了。Enum.Parse返回的值需要进行类型转换。这是一个奇怪的.NET。。。我将立即修复我的帖子。我将查看该代码。至于解析,我一直看到一条红色下划线,上面写着“无法将类型“object”隐式转换为“resolution”。存在显式转换(是否缺少转换?)…当我开始失去我的深度时,你知道我需要什么吗?很抱歉,
Enum.Parse
return的类型是
object
,你必须转换它。欢迎加入。
<ComboBox ItemsSource="{Binding Path=MyEnumValues}"
          SelectedItem="{Binding Path=SelectedEnum}"/>