Is there a built-in TypeConverter or UITypeEditor to edit a list of strings

前端 未结 2 1445
借酒劲吻你
借酒劲吻你 2020-12-21 05:03

I wish to know if .Net-3.5 comes with a built-in List or string[] TypeConverter or UITypeEditor so that I c

2条回答
  •  一向
    一向 (楼主)
    2020-12-21 05:35

    UITypeEditor for List

    For string[] you don't need to do anything special and the property grid will use a standard dialog containing a multi-line text box to edit string array and each line will be an element in the array.

    To edit List in property grid, you can use either of the following options:

    • StringCollectionEditor which shows a dialog containing a multi-line text box to edit elements
    • Create a custom CollectionEditor to edit items in a collection editor dialog

    Option 1 - StringCollectionEditor

    private List myList = new List();
    [Editor("System.Windows.Forms.Design.StringCollectionEditor, " +
        "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
        typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List MyList {
        get {
            return myList;
        }
        set {
            myList = value;
        }
    }
    

    Option 2 - Custom CollectionEditor

    First create the custom editor:

    //You need to add reference to System.Design
    public class MyStringCollectionEditor : CollectionEditor {
        public MyStringCollectionEditor() : base(type: typeof(List)) { }
        protected override object CreateInstance(Type itemType) {
            return string.Empty;
        }
    }
    

    Then decorate the property with the editor attribute:

    private List myList = new List();
    [Editor(typeof(MyStringCollectionEditor), typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List MyList {
        get {
            return myList;
        }
        set {
            myList = value;
        }
    }
    

提交回复
热议问题