如何使用可以在设计时编辑的集合制作 UserControl? [英] How to make a UserControl with a Collection that can be edited at design time?

查看:36
本文介绍了如何使用可以在设计时编辑的集合制作 UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个名为 PanelsList 的类,它基本上是一个 TabControl 顶部没有标题,因此页面只能以编程方式更改.每个选项卡"将是一个名为 PanelsListItem 的类的实例,该类派生自 Panel.我还创建了一个实现 ICollectionICollectionPanelsListItemCollection 类.所以我在 PanelsList 中添加了以下内容:

I'm making a class called PanelsList that is basically a TabControl without the headers on the top, so the pages can only be changed programmatically. Each "tab" will be and instance of a class called PanelsListItem that derives from Panel. I also made a class PanelsListItemCollection that implements ICollection and ICollection<PanelsListItem>. So I added the following to my PanelsList:

    private PanelsListItemCollection _Items;

    public PanelsListItemCollection Items
    {
        get { return _Items; }
        set { SetItems(value); }
    }

    private void SetItems(PanelsListItemCollection value)
    {
        if (_Items != value)
        {
            if (_Items != null) _Items.PanelsList= null;
            _Items = value;
            if (_Items != null) _Items.PanelsList= this;
        }
    }

我假设在构建并添加 PanelsList 到我的表单后,我将能够在设计时编辑 PanelsListItemCollection.但是,当我在 Properties 编辑器中单击属性 Items 上的..."按钮时,Object Collection Editor 会打开,但是 AddRemove 按钮被禁用.

I assumed that after building and adding a PanelsList to my form I would be able to edit the PanelsListItemCollection on design time. But when I click on the "..." button on the property Items in the Proeprties editor, the Object Collection Editor opens but the Add and Remove buttons are disabled.

当我添加一个属性List时东西{得到;放;} 到我的 PanelsList 我可以在设计时从 Stuff 添加和删除控件.我想知道我是否需要实现 IList 而不是 ICollection?

When I added a property List<Control> Stuff { get; set; } to my PanelsList I could add and remove controls from Stuff on design time. I wonder if I need to implement IList instead of ICollection?

我也尝试实现了 IList 但它没有修复它.

I just tried implementing also IList<PanelsListItem> but it didn't fix it.

推荐答案

为了让设计者自动支持一个集合,它必须实现非通用的IList接口 - IList 不会自动工作.这是因为默认集合编辑器依赖于了解每个项目的索引.与设计器兼容的另一个要求(您的代码已经满足)是公开您的集合的属性必须同时具有 getset 方法;设计人员在编辑期间制作您的收藏的临时副本,然后在用户单击确定"时将其分配给属性.

In order for a collection to be automatically supported by the designer, it must implement the non-generic IList interface - IList<T> will not automatically work. This is because the default collection editor relies on knowing the index of each item. The other requirement for compatibility with the designer (which your code already satisfies) is that the property exposing your collection must have both get and set methods; the designer makes a temporary copy of your collection during editing and then assigns it to the property when the user clicks OK.

如果默认设置不够好,您可以通过扩展UITypeEditor 类(在System.Drawing.Design 命名空间中)并修饰在您的代码中使用 EditorAttribute 属性,例如

If the default is not good enough, you can implement your own collection editor by extending the UITypeEditor class (in the System.Drawing.Design namespace) and decorating the property in your code with the EditorAttribute, e.g.

[Editor(typeof(MyCustomCollectionEditor), typeof(UITypeEditor))] 
public PanelsListItemCollection Items { /* ... */ }

您还可以扩展现有的 CollectionEditor 类,但该类仅向派生类公开非常有限的功能.

You can also extend the existing CollectionEditor class, but the class only exposes very limited functionality to derived classes.

这篇关于如何使用可以在设计时编辑的集合制作 UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆