How can I edit a collection of filenames in a property grid?
I have a collection of filenames in a C# class:
private List<string> m_files
public List<string> Files
{
开发者_如何转开发 get
{
return m_files;
}
set
{
m_files = value;
}
}
I want to be able to display and edit this collection in a property grid, specifically I would like to be able to add files to this collection with a standard FileDialog
. Which is the easiest way to accomplish this?
Use the EditorAttribute
to specify that this property is edited with a 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;
}
}
You could hijack the StringCollectionEditor for a cheap solution:
[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; }
}
But actually checking the files or using an OFD is going to require you writing your own UITypeEditor. Do keep in mind that the paths of the files at design time is in no way representative for the paths they'll have when you deploy your project.
精彩评论