编辑收集的designMode最简单的方法是什么? [英] Simplest way to edit a collection in DesignMode?

查看:139
本文介绍了编辑收集的designMode最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是编辑最简单的方法和持续喜欢收藏十进制[] 名单,其中,串> 中的WinForms设计师?

What is the easiest way to edit and persist a collection like decimal[] or List<string> in the WinForms designer?

第一个问题是,一个参数的构造函数是必要的。于是我做了一个简单的包装类: (在某些时候,这是像为MyObject&LT; T&GT; ,但的WinForms设计师code发电机不知道该如何处理呢)

The first problem is that a parameterless constructor is needed. So I made a simple wrapper class: (at some point this was like MyObject<T>, but the WinForms designercode generator didn't know how to handle it)

[Serializable()]
public class MyObject
{
      public MyObject() {}
      public decimal Value {get; set;}
}

在容器类,我们定义一个属性和CollectionEditor属性添加到它:

In the container class we define a property and add the CollectionEditor attribute to it:

public class MyContainer
{
      private List<MyObject> _col = new List<MyObject>();

      [Editor(typeof(CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
      public List<MyObject> Collection
      {
           get { return _col; }
           set { _col = value; }
      }
}

现在我试过各种基于答案,这里计算器和articless在codeproject.com事情:

Now I tried all sorts of things based on answers here on stackoverflow and articless on codeproject.com:

  • ArrayEditor十进制[]字段
  • 在自定义类型转换器用于为MyObject
  • 的名单自定义集合类
  • 在容器类
  • 在读/写性能
  • 在自定义编辑器实现的EditValue
  • 实施IComponent的接口为MyObject
  • 在试图加入DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
  • ArrayEditor with decimal[] field
  • Custom TypeConverter for MyObject
  • Custom Collection class for List
  • Read/Write property on the container class
  • Custom Editor with implementation for EditValue
  • Implement IComponent interface for MyObject
  • Tried adding DesignerSerializationVisibility(DesignerSerializationVisibility.Content)

我没有得到它的工作,这样

I did get it to work so that

  • 收集是的designMode
  • 可见
  • 的集合是可​​编辑的designMode
  • 新的项目可以被添加到集合中的designMode

然而,保存,关闭并重新打开窗体集合中的元素不会被保留。

However, by saving, closing and re-opening the form the elements in the collection are never persisted.

编辑: 汉斯给了我一些提示(他的评论在某种程度上走进无效)。我顺着他的指引和更新以上来源,可惜还是无法正常工作......

Hans gave me some tips (his comments somehow went into the void). I followed his guidelines and updated the source above, which unfortunately still doesn't work...

推荐答案

我建议,如果可能的话,你公开一个总汇属性,它是同类型为一个已经使用的框架,这样你就可以重用现有的集合编辑器。例如,如果你使用的 StringCollection 类,那么你可以做以下并重新使用现有的WinForms编辑器...

I recommend that if possible you expose a colletion property that is the same type as one already used in the framework and so you can reuse the existing collection editor. For example, if you use a StringCollection class then you can do the following and reuse the WinForms existing editor...

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor("System.Windows.Forms.Design.StringCollectionEditor, 
             System.Design, Version=2.0.0.0, Culture=neutral, 
             PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public StringCollection Items
    {
        get { return _myStringCollection; }
    }

另外,如果你能暴露一个的String [] ,然后做到这一点......

Alternatively if you can expose a string[] then do this...

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor("System.Windows.Forms.Design.StringArrayEditor, 
            System.Design, Version=2.0.0.0, Culture=neutral, 
            PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public string[] Lines
    {
        get { return _myStringArray; }
        set { myStringArray = value; }
    }

这篇关于编辑收集的designMode最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆