PropertyGrid 中布尔属性的自定义编辑器 (C#)

发布于 2024-09-27 17:27:00 字数 1008 浏览 8 评论 0原文

我想将自定义编辑器分配给 PropertyGrid 中的 boolean 属性。我正在使用标准 propertygrid(来自命名空间 System.Windows.Forms)。可以使用 UITypeEditor 类将自定义编辑器分配给属性。但是,据我所知,不可能将其用于 boolean 属性。

我尝试通过覆盖属性网格来解决这个问题,以便我可以手动添加项目。我可以通过以下代码添加一个具有自定义编辑器的 string 属性:

Properties.Item.Add("My Custom Editor", "", false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true);
Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor();

到目前为止一切顺利,出现了一个自定义编辑器(网格中带有一个按钮)。但是,当我通过将默认值设置为 false(见下文)将类型更改为 boolean 时,打开自定义编辑器的按钮不会出现。相反,会出现一个带有 true/false 的下拉菜单。

Properties.Item.Add("My Custom Editor", false, false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true);
Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor();

有人有解决方案吗?

感谢转发!

问候, 彼得

I want to assign a custom editor to a boolean property in a PropertyGrid. I'm using the standard propertygrid (from namespace System.Windows.Forms). It is possible to assign custom editors to properties using the UITypeEditor class. However, as far as I can see, it is not possible to use it for a boolean property.

I've tried to solve it by overriding the property grid so I can add items manually. I can add a string property that has a custom editor by the following piece of code:

Properties.Item.Add("My Custom Editor", "", false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true);
Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor();

So far so good, a custom editor appears (with a button in the grid). However, when I change the type to boolean by setting the default value on false (see below), the button to open the custom editor doesn't appear. Instead, a dropdown menu with true/false appears.

Properties.Item.Add("My Custom Editor", false, false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true);
Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor();

Does anyone has a solution for this?

Thanks in forward!

Regards,
Peter

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

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

发布评论

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

评论(1

戏舞 2024-10-04 17:27:00

Microsoft PropertyGrid 检查此标志以确定它是否显示下拉箭头(flag == true)或模式按钮(flag == false):

bool flag = gridEntryFromRow.NeedsDropDownButton | gridEntryFromRow.Enumerable;

如果 UITypeEditor 样式为 DropDown,则第一部分为 true;如果附加的 TypeConverter 的 GetStandardValuesSupported 返回 true。

您可以在 Reflector 的 PropertyGridView.SelectRow 中检查所有这些。

如果您能够将自定义 TypeConverter 附加到您的布尔值(我将从 BooleanConverter 派生它),其 GetStandardValuesSupported 方法被重写以返回 false,那么您将获得模态按钮。当然,您会失去标准值(例如,双击不会循环值),这是一种权衡。我很久以前就发现了这个问题,这就是为什么在我自己的 PropertyGrid 中我没有那么严格,即使定义了标准值,只要我将 ForceEditor 属性附加到属性,也会启用模式编辑器。

The Microsoft PropertyGrid checks this flag to determine if it displays a dropdown arrow (flag == true) or a modal button (flag == false):

bool flag = gridEntryFromRow.NeedsDropDownButton | gridEntryFromRow.Enumerable;

The first part is true if the UITypeEditor style is DropDown and the second part is true if the attached TypeConverter's GetStandardValuesSupported returns true.

You can check all this in PropertyGridView.SelectRow in Reflector.

If you are able to attach a custom TypeConverter to your boolean (I would derive it from BooleanConverter) whose GetStandardValuesSupported method is overriden to return false, then you will get your modal button. Of course you loose the standard values (double clicking won't cycle the values for example), this is a tradeoff. I have identified this issue a long time ago and that's why in my own PropertyGrid I am not so strict and will enable a modal editor even if standard values are defined as long as I attach a ForceEditor attribute to the property.

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