Simplest way to edit a collection in DesignMode?

孤街浪徒 提交于 2019-12-22 10:34:24

问题


What is the easiest way to edit and persist a collection like decimal[] or List<string> in the WinForms designer?

The first problem is that a parameterless constructor is needed. So I made a simple wrapper class: (at some point this was like MyObject<T>, but the WinForms designercode generator didn't know how to handle it)

[Serializable()]
public class MyObject
{
      public MyObject() {}
      public decimal Value {get; set;}
}

In the container class we define a property and add the CollectionEditor attribute to it:

public class MyContainer
{
      private List<MyObject> _col = new List<MyObject>();

      [Editor(typeof(CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
      public List<MyObject> Collection
      {
           get { return _col; }
           set { _col = value; }
      }
}

Now I tried all sorts of things based on answers here on stackoverflow and articless on codeproject.com:

  • ArrayEditor with decimal[] field
  • Custom TypeConverter for MyObject
  • Custom Collection class for List
  • Read/Write property on the container class
  • Custom Editor with implementation for EditValue
  • Implement IComponent interface for MyObject
  • Tried adding DesignerSerializationVisibility(DesignerSerializationVisibility.Content)

I did get it to work so that

  • The collection is visible in DesignMode
  • The collection is editable in DesignMode
  • New items can be added to the collection in DesignMode

However, by saving, closing and re-opening the form the elements in the collection are never persisted.

Edit: Hans gave me some tips (his comments somehow went into the void). I followed his guidelines and updated the source above, which unfortunately still doesn't work...


回答1:


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; }
    }



回答2:


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; }
}


来源:https://stackoverflow.com/questions/6493021/simplest-way-to-edit-a-collection-in-designmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!