简体   繁体   English

如何使用可在设计时编辑的Collection创建UserControl?

[英]How to make a UserControl with a Collection that can be edited at design time?

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. 我正在创建一个名为PanelsList的类,该类基本上是一个TabControl ,顶部没有标题,因此只能以编程方式更改页面。 Each "tab" will be and instance of a class called PanelsListItem that derives from Panel . 每个“选项卡”将是一个名为PanelsListItem的类的实例,该类派生自Panel I also made a class PanelsListItemCollection that implements ICollection and ICollection<PanelsListItem> . 我还做了一个类PanelsListItemCollection实现ICollectionICollection<PanelsListItem> So I added the following to my PanelsList : 所以我将以下内容添加到我的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;
        }
    }

I assumed that after building and adding a PanelsList to my form I would be able to edit the PanelsListItemCollection on design time. 我假定在构建并将PanelsList添加到我的表单之后,我将能够在设计时编辑PanelsListItemCollection 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. 但是,当我在Proeprties编辑器中单击属性Items上的“ ...”按钮时,将打开“ 对象集合编辑器”,但是“ 添加”和“ 删除”按钮被禁用。

When I added a property List<Control> Stuff { get; set; } 当我添加一个属性List<Control> Stuff { get; set; } List<Control> Stuff { get; set; } List<Control> Stuff { get; set; } to my PanelsList I could add and remove controls from Stuff on design time. List<Control> Stuff { get; set; }PanelsList我可以添加和删除从控制Stuff上的设计时间。 I wonder if I need to implement IList instead of ICollection ? 我想知道是否需要实现IList而不是ICollection吗?

Edit: I just tried implementing also IList<PanelsListItem> but it didn't fix it. 编辑:我刚刚尝试实现IList<PanelsListItem>但它没有修复它。

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. 为了使设计人员能够自动支持集合,它必须实现非通用的IList接口IList<T>将不会自动工作。 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; 与设计者兼容的另一个要求(您的代码已经满足了)是暴露集合的属性必须同时具有getset方法。 the designer makes a temporary copy of your collection during editing and then assigns it to the property when the user clicks OK. 设计人员在编辑期间制作集合的临时副本,然后在用户单击“确定”时将其分配给属性。

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 , eg 如果默认值不够好,您可以通过扩展UITypeEditor类(在System.Drawing.Design命名空间中)并使用EditorAttribute在代码中修饰属性来实现自己的集合编辑器,例如

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

You can also extend the existing CollectionEditor class, but the class only exposes very limited functionality to derived classes. 您还可以扩展现有的CollectionEditor类,但该类仅向派生类公开非常有限的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM