如下图所示,对于 ListView 控件,您可以使用“属性” Pane 添加项目。
如何为我的 UserControl 启用此类功能?
我在 Google 上搜索时没有得到任何结果,但我可能没有使用正确的术语。
有人知道吗?
谢谢
最佳答案
您需要创建一个类来定义集合 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/