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

ε祈祈猫儿з 提交于 2019-11-29 14:53:39

You can use [Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, [assembly version and public key token information here]", typeof(System.Drawing.Design.UITypeEditor))]

UITypeEditor for List<String>

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<string> 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<string> myList = new List<string>();
[Editor("System.Windows.Forms.Design.StringCollectionEditor, " +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> 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<String>)) { }
    protected override object CreateInstance(Type itemType) {
        return string.Empty;
    }
}

Then decorate the property with the editor attribute:

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

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