C# 全局变量/常量可绑定到 DDL,但具有“Nice”功能名字
由于通常的原因,我需要在应用程序中使用一些常量。
我考虑过的方法:
1)声明一个枚举:
public enum myOptions
{
MyOption1 = 72,
MyOption2 = 31,
MyOption3 = 44
}
虽然这很适合编程,而且我可以将枚举直接绑定到 DDL,但是当用户看到它们时,枚举“名称”很丑陋 - 用户会看到“我的选项1”,我希望他们看到“我的选项#1”。
2) 使用列表:
public static List<KeyValuePair<int, string>> myOptions = new List<KeyValuePair<int, string>>
{
new KeyValuePair<int, string>(77, "My Option #1"),
new KeyValuePair<int, string>(31, "My Option #2"),
new KeyValuePair<int, string>(44, "My Option #3")
}
因此,虽然这很好地绑定到 DDL,为我提供了一个很好的显示值以及整数返回值,但我没有任何东西来测试我的返回值。因此,例如:
if (selectedOption=????) //I'd have to hardcode the Key or Value I want to test for.
3)我可以构建漂亮的全局/常量程序集:
static myOptions
{
public static KeyValuePair<int, string> MyOption1 = new new KeyValuePair<int, string>(77, "My Option #1");
public static KeyValuePair<int, string> MyOption2 = new new KeyValuePair<int, string>(31, "My Option #2");
public static KeyValuePair<int, string> MyOption3 = new new KeyValuePair<int, string>(44, "My Option #3");
}
这给了我漂亮的显示名称,很适合编码,但据我所知,我无法轻松地将其绑定到 DDL(我必须手动编码)。
有没有人有一种优雅的方法来创建可以轻松绑定到 DDL 的常量,这样我就可以有一个漂亮的显示名称?
现在我唯一能想到的就是构建枚举和列表,这看起来很烦人。
For the usual reasons, I need to use some constants in my application.
The approaches I've considered:
1) Declare an Enum:
public enum myOptions
{
MyOption1 = 72,
MyOption2 = 31,
MyOption3 = 44
}
While this is good to program against, and I can bind the enum directly to a DDL, but the Enum "names" are ugly when the user sees them - The user will see "MyOption1" and I want them to see "My Option #1".
2) Use a List:
public static List<KeyValuePair<int, string>> myOptions = new List<KeyValuePair<int, string>>
{
new KeyValuePair<int, string>(77, "My Option #1"),
new KeyValuePair<int, string>(31, "My Option #2"),
new KeyValuePair<int, string>(44, "My Option #3")
}
So while this binds very nicely to a DDL, gives me a nice display value as well as an integer return value, I don't have anything to test my return value against. So, for example:
if (selectedOption=????) //I'd have to hardcode the Key or Value I want to test for.
3) I could build nice Global/Constants assembly:
static myOptions
{
public static KeyValuePair<int, string> MyOption1 = new new KeyValuePair<int, string>(77, "My Option #1");
public static KeyValuePair<int, string> MyOption2 = new new KeyValuePair<int, string>(31, "My Option #2");
public static KeyValuePair<int, string> MyOption3 = new new KeyValuePair<int, string>(44, "My Option #3");
}
Which gives me nice display names, is nice to code against, but as far as I can tell there's no way for me to easily bind this to a DDL (I'd have to hand code it).
Does anyone have an elegant way of creating constants that are easily bound against a DDL, where I can have a nice display name?
Right now the only thing I can think of is building BOTH the Enum and the List, which seems annoying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我总是倾向于装饰枚举值:
或者更好的是,您可以创建一个自定义属性,该属性将绑定到资源文件或配置设置,以便可以更改此数据,而无需重新编译
更新以一般从枚举中提取属性
I always lean toward decorated enum values:
or better yet you could create a custom attribute that would bind to a Resource file or a configuration setting so this data could be changed without recompiling
Update to generically pull attributes from an enum
根据 @hunter 提供的答案,我决定发布我的完整实现,因为我花了一段时间才把它弄好(仍在此处学习...)
在其他地方,要绑定到 DDL,您可以简单地执行以下操作
: ,要提取选定的值,您可以执行以下操作:
..而且我并没有就此停止。我扩展了 ComboBox 控件本身,因此现在绑定它非常方便。
所以现在,每当我想要填充 DDL 时,我只需:
代码绑定它,并选择与传入的 Enum 当前值匹配的项目。
欢迎对我的实现提出评论和批评(实际上,我会很感激- 就像我说的,我正在努力学习......)
Based on the answer provided by @hunter, I decided to post my full implementation since it took me a while to get it right (still learning here...)
Elsewhere, to do the binding to a DDL you can simply do this:
Finally, to pull the selected value out you can do this:
..and I didn't stop there. I extended the ComboBox control itself, so binding it now is super-convenient.
So now, whenever I want to populate a DDL, I just:
The code binds it, and selected the item that matches the current value of the Enum passed in.
Comments and criticism on my implementation is welcome (actually, I'd be thankful - like I said, I'm trying to learn...)
另一个建议?
然后您可以进行绑定,而无需创建枚举的实例。这是您想要有关信息的枚举类型,而不是特定实例)
Another Suggestion?
Then you can bind without having to create an instance of the enum. It is the type of enum you want information about, not a specific instance)