Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

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

I wish to know if .Net-3.5 comes with a built-in List<string> or string[] TypeConverter or UITypeEditor so that I can edit this kind of property from a property grid.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...