如何在 C# 中的 propertygrid 中向枚举选择添加工具提示?

发布于 2024-12-02 17:11:20 字数 227 浏览 3 评论 0原文

我刚刚了解了如何使用 PropertyGrid 的基本功能,并且发现我的一些枚举常量并不是很不言自明。当用户打开所有枚举常量的列表时,是否有可能将鼠标悬停在哪个常量上出现工具提示?

例如,如果我在 PropertyGrid 中有一个名为 SomeEnum 的属性,其值为 Enum1、Enum2、Enum3。当用户想要更改属性的值时,他拉下列表并将鼠标悬停在 Enum1 上,将出现一个工具提示,显示“这是 Enum1”等。

I just found out how to use the basic features of a PropertyGrid and I found that some of my enum constants aren't very self-explanatory. Is it possible that when the user opens the list of all the enum constants that a tooltip will appear for whichever constant he hovers his mouse over?

For example if I have a property in a PropertyGrid called SomeEnum and the values are Enum1, Enum2, Enum3. When the user wants to change the value of the property, he brings down the list and hovers over Enum1, a tooltip will appear saying "This is Enum1" and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2024-12-09 17:11:20

您可能想要覆盖枚举的默认转换为字符串功能,如这些 SO 帖子中所述:

C# String enums

C#:如何使用用于本地化枚举的类型转换器

或此 MSDN 文章:

http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter%28VS.80%29.aspx

You would want to override the default convert-to-string functionality of the enum as described in these S.O. posts:

C# String enums

C#: How to use a Type Converter to localize enums

or this MSDN article:

http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter%28VS.80%29.aspx

探春 2024-12-09 17:11:20

我不认为有任何简单的方法可以用 PropertyGrid 来完成您所要求的操作。你真正应该做的是重命名你的枚举值以更清楚地表达它们的目的;这样做的另一个好处是使您的源代码更易于理解。

如果您需要指定有关通过 PropertyGrid 设置的特定属性的附加信息,您可以通过使用 DescriptionAttribute 对其进行标记来实现:

[Description("This is my description of this property")]
public int Foo { get; set; }

如果您确实决定保持枚举值不变,则可以实现一个 TypeConverter 并使用 TypeConverterAttribute 标记使用特定枚举的每个属性;这将允许您显式指定枚举值和 PropertyGrid 中显示的文本之间的转换。有关更多详细信息,请参阅这篇 MSDN 文章

I don't believe there's any easy way to do what you're asking with a PropertyGrid. What you really should be doing is to rename your enumeration values to express their purposes more clearly; this has the added benefit of making your source code easier to understand.

If you need to specify additional information regarding a particular property that's being set through the PropertyGrid, you can do so by tagging it with a DescriptionAttribute:

[Description("This is my description of this property")]
public int Foo { get; set; }

If you're really dead set on keeping your enum values as they are, you could potentially implement a TypeConverter and flag each property that uses a particular enum with TypeConverterAttribute; this would let you explicitly specify a conversion between the enum values and the text that gets displayed in the PropertyGrid. See this MSDN article for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文