有没有办法在属性网格之外使用 CollectionEditor?

发布于 2024-09-25 10:42:54 字数 193 浏览 7 评论 0原文

我正在用一些可以让我更好地自定义 UI 的东西替换我的属性网格。我在表单上放置了一个按钮,希望单击该按钮时会弹出一个 CollectionEditor 并允许我修改我的代码。当我使用 PropertyGrid 时,我所需要做的就是向指向我的 CollectionEditor 的属性添加一些属性,它就起作用了。但是如何手动调用 CollectionEditor 呢?谢谢!

I'm replacing my property grid with something that will allow me to customize my UI a bit better. I placed a button on my form that I hope when clicked would pop up a CollectionEditor and allow me to modify my code. When I was using the PropertyGrid, all I needed to do was add some attributes to the property pointing to my CollectionEditor and it worked. But how do I invoke the CollectionEditor manually? Thanks!

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

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

发布评论

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

评论(2

鸠书 2024-10-02 10:42:54

在这里找到答案:http://www.devnewsgroups.net/windowsforms/t11948-collectioneditor。 aspx

以防万一上面链接的网站有一天消失,这就是它的要点。然而,该代码是从上面的链接逐字逐句获取的;评论是我的。

假设您有一个带有列表框和按钮的表单。如果您想使用 CollectionEditor 编辑 ListBox 中的项目,您将在 EventHandler 中执行以下操作:

private void button1_Click(object sender, System.EventArgs e)
{
    //listBox1 is the object containing the collection.  Remember, if the collection
    //belongs to the class you're editing, you can use this
    //Items is the name of the property that is the collection you wish to edit.
    PropertyDescriptor pd = TypeDescriptor.GetProperties(listBox1)["Items"];
    UITypeEditor editor = (UITypeEditor)pd.GetEditor(typeof(UITypeEditor));
    RuntimeServiceProvider serviceProvider = new RuntimeServiceProvider();
    editor.EditValue(serviceProvider, serviceProvider, listBox1.Items);
}

现在您需要做的下一件事是创建 RuntimeServiceProvider()。这是上面链接中的发帖人为实现此目的而编写的代码:

public class RuntimeServiceProvider : IServiceProvider, ITypeDescriptorContext
{
    #region IServiceProvider Members

    object IServiceProvider.GetService(Type serviceType)
    {
        if (serviceType == typeof(IWindowsFormsEditorService))
        {
            return new WindowsFormsEditorService();
        }

        return null;
    }

    class WindowsFormsEditorService : IWindowsFormsEditorService
    {
        #region IWindowsFormsEditorService Members

        public void DropDownControl(Control control)
        {
        }

        public void CloseDropDown()
        {
        }

        public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
        {
            return dialog.ShowDialog();
        }

        #endregion
    }

    #endregion

    #region ITypeDescriptorContext Members

    public void OnComponentChanged()
    {
    }

    public IContainer Container
    {
        get { return null; }
    }

    public bool OnComponentChanging()
    {
        return true; // true to keep changes, otherwise false
    }

    public object Instance
    {
        get { return null; }
    }

    public PropertyDescriptor PropertyDescriptor
    {
        get { return null; }
    }

    #endregion
}

Found the answer here: http://www.devnewsgroups.net/windowsforms/t11948-collectioneditor.aspx

Just in case the site linked above goes away some day, here's the gist of it. The code is verbatim from the link above, however; the comments are mine.

Assume you have a form with a ListBox and a button. If you wanted to edit the items in the ListBox using the CollectionEditor, you would do the following in your EventHandler:

private void button1_Click(object sender, System.EventArgs e)
{
    //listBox1 is the object containing the collection.  Remember, if the collection
    //belongs to the class you're editing, you can use this
    //Items is the name of the property that is the collection you wish to edit.
    PropertyDescriptor pd = TypeDescriptor.GetProperties(listBox1)["Items"];
    UITypeEditor editor = (UITypeEditor)pd.GetEditor(typeof(UITypeEditor));
    RuntimeServiceProvider serviceProvider = new RuntimeServiceProvider();
    editor.EditValue(serviceProvider, serviceProvider, listBox1.Items);
}

Now the next thing you need to do is to create the RuntimeServiceProvider(). This is the code the poster in the link above wrote to implement this:

public class RuntimeServiceProvider : IServiceProvider, ITypeDescriptorContext
{
    #region IServiceProvider Members

    object IServiceProvider.GetService(Type serviceType)
    {
        if (serviceType == typeof(IWindowsFormsEditorService))
        {
            return new WindowsFormsEditorService();
        }

        return null;
    }

    class WindowsFormsEditorService : IWindowsFormsEditorService
    {
        #region IWindowsFormsEditorService Members

        public void DropDownControl(Control control)
        {
        }

        public void CloseDropDown()
        {
        }

        public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
        {
            return dialog.ShowDialog();
        }

        #endregion
    }

    #endregion

    #region ITypeDescriptorContext Members

    public void OnComponentChanged()
    {
    }

    public IContainer Container
    {
        get { return null; }
    }

    public bool OnComponentChanging()
    {
        return true; // true to keep changes, otherwise false
    }

    public object Instance
    {
        get { return null; }
    }

    public PropertyDescriptor PropertyDescriptor
    {
        get { return null; }
    }

    #endregion
}
久夏青 2024-10-02 10:42:54

由于我无法发表评论,因此我将在此发布:

您可以通过在 WindowsFormsEditorService.ShowDialog 中的 CollectionEditor 的 okButton 上添加 Click 事件来获取 DialogResult

public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
    ((System.Windows.Forms.Button)dialog.Controls.Find("okButton", true)[0]).Click += WindowsFormsEditorService_Click;
    return dialog.ShowDialog();
}

...

private void WindowsFormsEditorService_Click(object sender, EventArgs e)
{
    dr = DialogResult.OK;
}

Since I can't comment i'll post this here:

You can get the DialogResult by adding a Click event on the okButton of the CollectionEditor in WindowsFormsEditorService.ShowDialog

public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
    ((System.Windows.Forms.Button)dialog.Controls.Find("okButton", true)[0]).Click += WindowsFormsEditorService_Click;
    return dialog.ShowDialog();
}

...

private void WindowsFormsEditorService_Click(object sender, EventArgs e)
{
    dr = DialogResult.OK;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文