枚举到下拉框到方法
基本上,这样做的最佳方法是什么?
我有一个枚举,它具有所有可能的分辨率,我希望它们显示在下拉组合框中。
到目前为止,我发现我可以将枚举绑定到组合框,例如:
comboBox2.DataSource = Enum.GetNames(typeof(Resolution));
但是,在一个方法中,我有:
public void testmethod(Resolution res){}
并且我想不出一种转换回来的方法。我正在考虑更改方法以使用字符串,但随后我必须在方法中执行 case
或 if
才能转换回枚举。
此外,我理想地希望某些名称带有空格。我已经阅读过有关 [Description("Description with space")]
的内容,但我认为这只适用于 ToString。
即使我要执行某种循环并通过 ToString 将每个项目添加到框中,它仍然会返回一个字符串。
除了将枚举全部转储并采用不同的方法之外,我不太确定如何继续。
我只是想知道,如果你遇到类似的情况,你会怎么做?
Basically, what is the best way of doing this?
I have an Enum which has all the possible resolutions and I want them to be displayed on a drop down combobox.
So far, I found I could bind the enum to the combobox like:
comboBox2.DataSource = Enum.GetNames(typeof(Resolution));
However, in a method, I have:
public void testmethod(Resolution res){}
and I can't think of a way to convert back. I was thinking of changing the method to use a string, but then I will have to do a case
or if
s in the method to convert back to the enum.
In addition, I ideally want some of the names to have spaces. I have read about the [Description("Description with spaces")]
but I think this only gets applied on ToString.
Even if I was to do some sort of loop and add each item to the box via ToString, it will still return a string.
I am not really sure how to proceed other than to dump the Enum all together and just go for a different approach.
I was just wondering in a similar situation, what would you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将使用
LookupEdit
代替,并将 Enum Value 绑定到键,并将Enum.GetNames(typeof(Resolutions));
绑定到编辑时显示的值。然后,当用户选择一个项目时,您将获得实际值而不是名称。I would use a
LookupEdit
instead, and tie the Enum Value to the Key and theEnum.GetNames(typeof(Resolutions));
to the displayed value on the edit. Then when the user selects an item you get the actual value instead of the name.我会使用某种映射 - 每个枚举值都有自己的字符串描述。
其代码可以是:
I would go with some sort of map - each enumeration value will have its own string description.
Code for this can be:
难道你不能只做一个
Enum.Parse(typeof(Resolution),comboBox2.SelectedText)
吗?因此,您对
testmethod
的调用将如下所示:假设组合框的
DropDownStyle
设置为 DropDownList。Can't you just do a
Enum.Parse(typeof(Resolution), comboBox2.SelectedText)
?So your call to
testmethod
would look like:Assuming that the combo box's
DropDownStyle
is set to DropDownList.您可以使用
Enum.TryParse
:You can use
Enum.TryParse<TEnum>
:您可以使用
Enum.Parse(Type t, string s)
方法从字符串中获取枚举,在您的情况下,它是:关于您的描述想法,我在代码中使用以下内容:
当然,您必须记住使用扩展方法来获取描述 - 然而将其转换回来是不同的。
You can use the
Enum.Parse(Type t, string s)
method to get an enum from a string, in your case it would be:Concerning your description idea, I use the following in my code:
Of course, you'll have to remember to use the extension method to get the description - converting it back is something different however.
填充要绑定的可观察集合。
然后在 xaml 中进行绑定:
请注意,从技术上讲,由于列表在运行时不会更改,因此我们不需要
ObservableCollection
,而List
就可以,但我认为ObservableCollection< /code> 是使用虚拟机时要养成的好习惯。
Populate an Observable Collection to bind to.
Then in xaml to bind:
Note that technically as the list is not changing at runtime, we do not need
ObservableCollection
aList
would do, but I thinkObservableCollection
is a good habit to get into when working with VMs.