简体   繁体   中英

StringCollection editor does not store the values entered by user

I have implemented StringCollection editor in my custom control and below is the code :

[Description("extra free-form attributes on this thing.")]
[Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
   typeof(System.Drawing.Design.UITypeEditor))]
public System.Collections.Specialized.StringCollection Items
{
   get
   {
      if (items == null)
         items = new System.Collections.Specialized.StringCollection();

      return  this.items;
   }
}

public System.Collections.Specialized.StringCollection items;

This works fine but every time i enter some value in the collection and re-open it.. values are lost ie it does not store the values.

Is there anything missing to store the value of user entered strings or do i need to implement custom StringCollection so that user entered string values persist in the String Editor.

I even refered to below given link.. but still issue exists : How can I use a WinForms PropertyGrid to edit a list of strings?

Yes, you need to apply DesignerSerializationVisibility attribute to DesignerSerializationVisibility.Content . Without that all changes to the complex objects(other than primitives, strings, etc) will be lost.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("extra free-form attributes on this thing.")]
[Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
   typeof(System.Drawing.Design.UITypeEditor))]
public System.Collections.Specialized.StringCollection Items
{
   get
   {
      if (items == null)
         items = new System.Collections.Specialized.StringCollection();

      return  this.items;
   }
}

You might also try creating the list in your constructor. That along with the string collection editor and DesignerSerializationVisibility attributes works for me.

[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> TestList { get; set; }

public ListTest()
{
    TestList = new List<string>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM