在运行时创建/修改枚举 [英] Creating / Modifying Enums at Runtime

查看:184
本文介绍了在运行时创建/修改枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建用户具有创建最终将显示在的PropertyGrid 自己的自定义属性的选择方案。现在我不想用自定义编辑器的一塌糊涂,所以我只允许基本类型属性(字符串 INT 双击的DateTime 布尔等)该的PropertyGrid 已经有了内置的编辑器。

I'm creating a program where the user has the option of creating their own custom properties that will ultimately be displayed in a PropertyGrid. Right now I don't want to mess with custom editors, so I'm only allowing primitive type properties (string, int, double, DateTime, bool etc.) that the PropertyGrid already has built in editors for.

不过,我也希望给用户的选项创建在那里他们可以定义可能值的列表,这反过来将显示为一个下拉的的PropertyGrid

However, I also want to give the user the option to create multiple choice properties where they can defined a list of possible values which in turn will show up as a drop down list in the PropertyGrid.

当我硬代码枚举在我的代码属性网格自动显示的是性能枚举作为一个下拉列表中。但是,我可以创建和或者在运行时修改枚举,使用户可以在下拉添加另一个属性选项,然后回到的PropertyGrid ,看看他们的新选项下来?

When I hard code an Enum in my code the property grid automatically shows properties of that enum as a drop down list. But can I create and or modify an enumeration at runtime so the user could add another property option, and go back to the PropertyGrid and see their new option in a drop down?

更新

考虑帕特里克评论,我在想,枚举是不是在这种情况下,以正确的方式去。所以不是我该如何使用字符串列表来填充下拉在的PropertyGrid 项目? ?请问需要自定义编辑器

Considering Patricks comment, I'm thinking that Enums are not the right way to go in this case. So instead how can I use a list of strings to populate a drop down in a PropertyGrid item? Would that require a custom editor?

推荐答案

答案是一个简单的类:的TypeConverter 。 (是的,枚举不适合这里)。

The answer is in a simple class: TypeConverter. (and yes, enums are not suitable here).

由于我没有很多细节,我会假设你有一个PropertyGrid的链接,以一个目标实例由SelectedObject属性和目标实例实现ICustomTypeDescriptor,这样就可以在运行时添加的属性(即的PropertyDescriptor)。我不知道你的设计,但如果你不是做这样的,我劝你还是看看吧。

Since I have not a lot of details, I will assume that you have a PropertyGrid "linked" to a target instance by the SelectedObject property and that your target instance implements ICustomTypeDescriptor so that you can add properties (i.e. PropertyDescriptors) at runtime. I don't know your design but if you are not doing like this, I advise you to have a look at it.

现在让我们假设你添加一个字符串属性而且你想要让你的用户指定的一组约束此属性。你的UI让我们的用户输入一组字符串,你会得到的字符串作为一个结果列表。也许你在你的目标实例属性的字典,以便让我们假设这个新的列表储存在那里了。

Now let's say that you add a string property and that you want to let your user specify a set of constraints for this property. Your UI let's the user enter a set of strings and you get a list of strings as a result. Maybe you keep a dictionary of properties in your target instance so let's assume this new list is stored there too.

现在,只写自TypeConverter(或StringConverter派生的新转换器也许在这个例子中)。你将不得不重写GetStandardValuesSupported返回true和 GetStandardValues 返回字符串列表(使用上下文参数访问实例属性和它的字符串列表)。此转换器将通过与PropertyDescriptor.Converter财产您的PropertyDescriptor予以公布。

Now, just write a new converter derived from TypeConverter (or StringConverter maybe in this example). You will have to override GetStandardValuesSupported to return true and GetStandardValues to return the list of strings (use the context parameter to access the Instance property and its list of strings). This converter will be published by your PropertyDescriptor with the PropertyDescriptor.Converter property.

我希望这不是太含糊不清。如果你对这个过程的一个具体问题,只是让我知道。

I hope this is not too nebulous. If you have a specific question on this process, just let me know.

这篇关于在运行时创建/修改枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆