【问题标题】:StringCollection editor does not store the values entered by userStringCollection 编辑器不存储用户输入的值
【发布时间】:2014-08-22 10:50:58
【问题描述】:

我已经在我的自定义控件中实现了 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;

这工作正常,但每次我在集合中输入一些值并重新打开它时。值都会丢失,即它不存储值。

是否缺少任何东西来存储用户输入的字符串的值,或者我需要实现自定义 StringCollection 以便用户输入的字符串值保留在字符串编辑器中。

我什至参考了下面给出的链接..但仍然存在问题: How can I use a WinForms PropertyGrid to edit a list of strings?

【问题讨论】:

    标签: c# .net windows listbox stringcollection


    【解决方案1】:

    是的,您需要将DesignerSerializationVisibility 属性应用于DesignerSerializationVisibility.Content。否则,对复杂对象的所有更改(除了原语、字符串等)都将丢失。

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

    【讨论】:

    • 按照建议..我尝试了上面给出的选项..但仍然存在问题。
    • @user1291401 刚刚测试过,对我来说效果很好。您使用哪个版本的 VisualStudio?尝试清理解决方案并重建它。然后检查一下,这可能会有所帮助...
    • @ Sriram Sakthivel - VS 2012 甚至尝试清理,重建解决方案.. 仍然存在问题。甚至它也没有反映在列表框中。
    • @user1291401 你说的是什么意思,即使它也没有反映在列表框中?什么列表框?发布一个完整的示例来演示该问题。否则很难提供帮助,因为它对我有用。
    • 我已经实现了自定义控件.. 类似于 ListBox。现在我正在使用 StringCollection 编辑器,以便用户可以在编辑器中输入字符串,它应该存储/反映在我的自定义控件中。但是每次我单击 StringCollection Editor 时,它总是为空,即使上次我在其中输入了几个字符串值。因此我提到了它——它没有反映在列表框中。
    【解决方案2】:

    您也可以尝试在构造函数中创建列表。这与字符串集合编辑器和 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>();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2011-05-09
      相关资源
      最近更新 更多