Simplest way to edit a collection in DesignMode?

↘锁芯ラ 提交于 2019-12-05 22:40:03

I recommend that if possible you expose a colletion property that is the same type as one already used in the framework and so you can reuse the existing collection editor. For example, if you use a StringCollection class then you can do the following and reuse the WinForms existing editor...

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor("System.Windows.Forms.Design.StringCollectionEditor, 
             System.Design, Version=2.0.0.0, Culture=neutral, 
             PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public StringCollection Items
    {
        get { return _myStringCollection; }
    }

Alternatively if you can expose a string[] then do this...

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor("System.Windows.Forms.Design.StringArrayEditor, 
            System.Design, Version=2.0.0.0, Culture=neutral, 
            PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public string[] Lines
    {
        get { return _myStringArray; }
        set { myStringArray = value; }
    }

Example with List of Objects property:


public partial class SCon : UserControl
{
    public SCon()
    {
        InitializeComponent();
        if (Persoanas == null)
        {
            Persoanas = new List<Persoana>();
        }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<Persoan> Persoanas { get; set; }

}

[Serializable]
public class Persoan   
{
    public int Id { get; set; }
    public String Name { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!