如何创建与属性网格中使用自定义集合编辑器的形式?自定义、网格、编辑器、属性

2023-09-03 00:38:41 作者:闪现放空大

我试图将属性网格控制,有另一个类的属性之一的列表/集合类。让我们称他们为A类和列表将包含B类,以供参考。

I am trying to incorporate a property grid control 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.

我想结合的形式,有两个列表框。左边的列表框将包含在我的节目当前未在右边的列表中的所有类B的的列表。右侧的列表将包含所有当前与A级相关联的B类的我要在这两者之间的两个列表之间移动项目的按钮。

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.

任何人都可以点我朝着正确的方向?

Can anyone point me in the right direction?

还有,我怎么能去大鱼大肉建立一个下拉为包含的ID列表的,如果有人可以点我的方向,用于完成此以及选择属性。

And also, how can I go about having setting up a drop down for a property that contains a list of id's to select from if anyone could point me in the direction for accomplishing this as well.

推荐答案

好了,我终于可以追踪如何做到这一点。

Okay, I was finally able to track down how to accomplish this.

我试图创建一个自定义CollectionEditor.CollectionForm这是接近我需要做的,但它是不太正确的方向。

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.

首先,创建一个包含您的GUI编辑您的收藏一个普通的Windows窗体。然后就包括其在表格返回的DialogResult按钮/按键。

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.

现在的关键是实现我一直在寻找的,是不是因为我原以为会是正确的做法,而是一个UITypeEditor的一个CollectionEditor.CollectionForm。

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 a UITypeEditor.

所以,我创建了一个从UITypeEditor的继承一个类。然后你只需充实它作为这样的:

So, I created a class that inherited from the UITypeEditor. Then you simply flesh it out as such:

public class CustomCollectionModalEditor: UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        if (context ==null || context.Instance == null)                
            return base.GetEditStyle(context);

        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService editorService;

        if (context == null || context.Instance == null || provider == null)
            return value;

        editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        CForm CollectionEditor = new CForm();

        if (editorService.ShowDialog(CollectionEditor) == System.Windows.Forms.DialogResult.OK)
            return CollectionEditor.Programmed;

        return value;
        //return base.EditValue(context, provider, value);
    }
}

关键部位注意的是功能GetEditStyle和的EditValue。负责发射了创建编辑您收集表的部分是在的EditValue覆盖功能。

The key parts to take note of are the functions GetEditStyle and EditValue. The part responsible for firing off the Form you created to edit your collection is in the EditValue override function.

的CForm是自定义集合编辑器的形式,我设计的这个测试编辑集合。你需要获取与IServiceProvider的相关IWindowsFormsEditorService和简单的调用IWindowsFormsEditorService的.ShowDialog(formVariable)为了向您展示设计,编辑集合的形式。然后,您可从您的形式返回的DialogResult值,并执行您所需要的任何自定义操作。

CForm is the custom collection editor form I designed in this test to edit the collection. You need to fetch the IWindowsFormsEditorService associated with the IServiceProvider and simply call the .ShowDialog(formVariable) of the IWindowsFormsEditorService in order to show the form you designed to edit the collection. You can then catch the returned DialogResult 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.