简体   繁体   English

StringCollection编辑器不存储用户输入的值

[英]StringCollection editor does not store the values entered by user

I have implemented StringCollection editor in my custom control and below is the code : 我已经在我的自定义控件中实现了StringCollection编辑器,下面是代码:

[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. 存储用户输入的字符串的值时是否缺少任何内容,或者我是否需要实现自定义StringCollection以便用户输入的字符串值在字符串编辑器中保持不变。

I even refered to below given link.. but still issue exists : How can I use a WinForms PropertyGrid to edit a list of strings? 我什至参考以下给定的链接..但仍然存在问题: 如何使用WinForms PropertyGrid编辑字符串列表?

Yes, you need to apply DesignerSerializationVisibility attribute to DesignerSerializationVisibility.Content . 是的,您需要将DesignerSerializationVisibility属性应用于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. 这与字符串集合编辑器和DesignerSerializationVisibility属性一起适用于我。

[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>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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