【问题标题】:Item Collection option for a User Control用户控件的项目集合选项
【发布时间】:2011-06-27 09:14:56
【问题描述】:

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

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

我在 Google 搜索时没有得到任何信息,但我可能没有使用正确的字词。

有人知道吗?

谢谢

【问题讨论】:

  • 你想将 Collection 传递给 usercontrol。
  • 我尝试搜索“将集合传递给 usercontrol”和一些变体,但只找到了对 WPF 和 Web 应用程序的引用,但这是针对 winforms 的。

标签: c# .net winforms properties controls


【解决方案1】:

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

您现在有了一个简单的界面,允许您浏览和编辑集合。还有很多不足之处,但要做得更多,您必须了解难以理解和实施的定制设计器。

【讨论】:

  • 哇,这太容易理解了。感谢您的帮助和时间! :)
  • @Sisyphus 你能举一个 MyItemType 的例子吗?
猜你喜欢
  • 2011-03-06
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多