c# - 用户控件的项目集合选项

标签 c# .net winforms properties controls

如下图所示,对于 ListView 控件,您可以使用“属性” Pane 添加项目。

如何为我的 UserControl 启用此类功能?

我在 Google 上搜索时没有得到任何结果,但我可能没有使用正确的术语。

有人知道吗?

谢谢

Visual Studio Properties Pane

最佳答案

您需要创建一个类来定义集合 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);
    }
}

最后你需要为你的用户控件添加一个集合的成员变量并适本地装饰它:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyItemTypeCollection MyItemTypes
    {
        get { return _myItemTypeCollection; }
    }

您现在拥有一个简单的界面,可让您浏览和编辑收藏。仍有许多不足之处,但要做得更多,您将不得不学习定制设计器,这可能难以理解和实现。

关于c# - 用户控件的项目集合选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5000331/

相关文章:

c# - 为什么复制流然后使用 BinaryFormatter 反序列化比仅仅反序列化更快

c# - 无法将正在执行的程序集加载到新的 AppDomain 中,FileNotFoundException

c# - SSMS 可扩展性项目 - 如何研究/调试

.net - 我的网站被 Statcounter 入侵了! Statcounter 是否保留 cookie 的记录?

c# - 如何创建可同时用于 C++ 和 C# 的 dll?

c# - 在不从缓冲区中删除其他按键的情况下拦截 ESC

c# - 自动完成并阻止新输入 - 组合框

c# - 如何使用 CefSharp WinForms 更改 URL

.net - WPF、Windows 窗体还是其他?

c# - MySQL 连接器在枚举后面附加比实际值小一的值