创建一个自定义 CollectionEditor 表单以与 PropertyGrid 一起使用?
我正在尝试合并 PropertyGrid
基于 CollectionEditor 类和 表单,其中一个类具有另一个类的列表/集合作为属性之一。我们称它们为 A 类,列表将包含 B 类以供参考。
我想合并一个具有两个列表框的表单。左侧的列表框将包含我的程序中当前不在右侧列表中的所有 B 类的列表。右侧的列表将包含当前与 A 类关联的所有 B 类。我想要中间的按钮在两个列表之间移动项目。
这很容易设计,但我不确定如何设置用作集合编辑器的表单。
I am trying to incorporate a PropertyGrid
control based off of the CollectionEditor class and Form, with a class that has a list/collection of another class as one of the properties. Lets call them class A and the list would be containing class B for reference.
I was wanting to incorporate a form that had two list boxes. The list box on the left would contain a list of all of class B's in my program that are not currently in the list on the right. The list on the right would contain all of the class B's that are currently associated with class A. I want buttons in between to move items between the two lists.
This would be easy to design, but I'm not sure exactly how to set up the form to be used as the collection editor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我终于能够找到如何实现这一目标。
我试图创建一个自定义
CollectionEditor.CollectionForm
这接近我需要做的事情,但它不是完全正确的方向。首先,创建一个常规 Windows 窗体,其中包含用于编辑集合的 GUI。然后只需在表单中包含返回 DialogResult 的一个或多个按钮即可。
现在,实现我所寻找的目标的关键不是
CollectionEditor.CollectionForm
(我原以为是正确的方法),而是UITypeEditor
。因此,我创建了一个继承自 UITypeEditor 的类。然后,您只需将其充实如下:
需要注意的关键部分是函数
GetEditStyle
和EditValue
。负责触发您创建的用于编辑集合的表单的部分位于EditValue
覆盖函数中。CForm
是我在本次测试中设计的用于编辑集合的自定义集合编辑器表单。您需要获取与IServiceProvider
关联的IWindowsFormsEditorService
并简单地调用IWindowsFormsEditorService
的.ShowDialog(formVariable)
以显示您设计的编辑集合的形式。然后,您可以从表单中捕获
返回的DialogResult
值并执行您需要的任何自定义处理。我希望这对某人有所帮助,因为我花了相当多的时间来确定合并此内容的正确方法。
Okay, I was finally able to track down how to accomplish this.
I was attempting to create a custom
CollectionEditor.CollectionForm
which was close to what I needed to do, but it wasn't quite the right direction.First of all, create a regular Windows Form which includes your GUI for editing your collection. Then just include button/buttons which return a DialogResult in the Form.
Now the key to accomplishing what I was looking for is not a
CollectionEditor.CollectionForm
as I had thought would be the correct approach, but rather aUITypeEditor
.So, I created a class that inherited from the UITypeEditor. Then you simply flesh it out as such:
The key parts to take note of, are the functions
GetEditStyle
andEditValue
. The part responsible for firing-off the Form you created to edit your collection, is in theEditValue
override function.CForm
is the custom collection editor form I designed in this test to edit the collection. You need to fetch theIWindowsFormsEditorService
associated with theIServiceProvider
and simply call the.ShowDialog(formVariable)
of theIWindowsFormsEditorService
in order to show the form you designed to edit the collection. You can thencatch
the returnedDialogResult
value from your Form and perform any custom handling that you require.I hope this helps someone out as it took me quite a bit of digging to determine the right way to incorporate this.
这回答了布兰登的问题。我也花了很长时间努力寻找如何实际替换默认的 propertygrid 集合编辑器。内森的回答就是最终的解决方案。布兰登(Brandon)介绍了我如何使用内森(Nathan)的解决方案并使用我自己的集合编辑器。
This answers Brandon's question. I too searched long and hard on how to actually replace the default propertygrid collection editor. Nathan's answer was the final solution. Brandon here is how I was able to use Nathan's solution and use my own collection editor.