简体   繁体   English

C#PropertyGrid编辑带有枚举键的数组

[英]c# PropertyGrid Edit array with enum keys

I have a property which is 我有一个物业

int [] MyProperty {get;set;}

I have a list of enums 我有一个枚举列表

enum MyEnums
{
    ENUM1=0
    ENUM2=1
    ENUM3=2
}

The property MUST be an int array due to tools that run on the code serializing the property out to a file. 由于必须在将属性序列化为文件的代码上运行的工具,所以该属性必须为int数组。 I'd like my property grid to allow me to edit MyProperty displaying the enum names as the keys rather than 0,1 and 2. The array must be the same length as the enum list so no members can be added or removed. 我希望我的属性网格允许我编辑MyProperty,将枚举名称显示为键,而不是0,1和2。该数组的长度必须与枚举列表相同,因此不能添加或删除成员。 I imagine I need a custom editor to achieve this? 我想我需要一个自定义编辑器来实现这一目标? Can anyone make any suggestions of the best way to achieve this? 谁能对实现这一目标的最佳方法提出任何建议?

Many thanks 非常感谢

You should use a dictionary in this case 在这种情况下,您应该使用字典

Dictionary<MyEnums, int> MyProperty {get; set;}

Also, you can implement your own "wrapper" class and and add an indexer for it using MyEnums http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx 另外,您可以实现自己的“包装器”类,并使用MyEnums http://msdn.microsoft.com/zh-cn/library/6x16t2tx.aspx添加索引器

You can use ToString for enum values. 您可以将ToString用作枚举值。 Also it seems your direction about the property editing is not that corrent. 而且似乎您对属性编辑的方向也不是那么好。 You can create a class for example: 您可以创建一个类,例如:

class EnumPropHolder
{
   MyEnums Prop1 = MyEnums.Enum1 {get;set;}
   MyEnums Prop2 = MyEnums.Enum2 {get;set;}
   //etc.
}

so now this makes you able to use your class instance as the object for the PropertyGrid in the way you are talking about 所以现在这使您能够按照您谈论的方式将类实例用作PropertyGrid的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM