【问题标题】:How can I edit a collection of filenames in a property grid?如何在属性网格中编辑文件名集合?
【发布时间】:2010-11-09 13:35:26
【问题描述】:

我在 C# 类中有一组文件名:

    private List<string> m_files

    public List<string> Files
    {
        get
        {
            return m_files;
        }
        set
        {
            m_files = value;
        }
    }

我希望能够在属性网格中显示和编辑此集合,特别是我希望能够使用标准FileDialog 将文件添加到此集合中。哪种方法最简单?

【问题讨论】:

    标签: c# collections propertygrid


    【解决方案1】:

    使用EditorAttribute 指定使用CollectionEditor 编辑此属性:

    private List<string> m_files
    
    [EditorAttribute(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public List<string> Files
    {
        get
        {
            return m_files;
        }
        set
        {
            m_files = value;
        }
    }
    

    【讨论】:

    【解决方案2】:

    您可以劫持 StringCollectionEditor 以获得便宜的解决方案:

        [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public List<string> Files {
            get { return m_files; }
            set { m_files = value; }
        }
    

    但实际上检查文件或使用 OFD 将需要您编写自己的 UITypeEditor。请记住,设计时文件的路径并不代表您部署项目时的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多