用户控件的项目集合选项

4 .net c# controls properties winforms

如下图所示,对于ListView控件,您可以使用"属性"窗格添加项目.

如何为UserControl启用此类内容?

当我搜索Google时,我没有得到任何东西,但我可能没有使用正确的条款.

有人知道吗?

谢谢

Visual Studio属性窗格

Sis*_*hus 6

您需要创建一个类来定义集合ID组成的对象类型.listView具有ListViewItem对象.TabControl具有TabPage对象.您的控件具有由您定义的对象.我们称之为MyItemType.

您还需要一个用于集合的包装类.一个简单的实现如下所示.

public class MyItemTypeCollection : CollectionBase
{

    public MyItemType this[int Index]
    {
        get
        {
            return (MyItemType)List[Index];
        }
    }

    public bool Contains(MyItemType itemType)
    {
        return List.Contains(itemType);
    }

    public int Add(MyItemType itemType)
    {
        return List.Add(itemType);
    }

    public void Remove(MyItemType itemType)
    {
        List.Remove(itemType);
    }

    public void Insert(int index, MyItemType itemType)
    {
        List.Insert(index, itemType);
    }

    public int IndexOf(MyItemType itemType)
    {
       return List.IndexOf(itemType);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,您需要将集合的成员变量添加到用户控件并正确装饰它:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyItemTypeCollection MyItemTypes
    {
        get { return _myItemTypeCollection; }
    }
Run Code Online (Sandbox Code Playgroud)

现在您有了一个简单的界面,允许您浏览和编辑集合.还有很多不足之处,但要做得更多,你将不得不学习很难理解和实施的定制设计师.