Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类列表的C#属性_C#_Visual Studio 2008_List_Class_Propertygrid - Fatal编程技术网

类列表的C#属性

类列表的C#属性,c#,visual-studio-2008,list,class,propertygrid,C#,Visual Studio 2008,List,Class,Propertygrid,我正在尝试使用VisualStudio设计器中的属性网格 我有一个类列表,我希望开发人员能够在设计时添加到其中,以便用户能够访问额外的功能 下面是一些我已经在代码中的示例代码。问题是,当开发人员进入设计模式时,他只能看到列表中有x个值,但无法看到任何细节。当尝试向列表中添加新项目时,用户将看到一个错误 找不到类型为“EditorTextBox+SyntaxRegex”的构造函数 现在代码是: private List<SyntaxRegex> _syntaxRegexList = n

我正在尝试使用VisualStudio设计器中的属性网格

我有一个类列表,我希望开发人员能够在设计时添加到其中,以便用户能够访问额外的功能

下面是一些我已经在代码中的示例代码。问题是,当开发人员进入设计模式时,他只能看到列表中有x个值,但无法看到任何细节。当尝试向列表中添加新项目时,用户将看到一个错误

找不到类型为“EditorTextBox+SyntaxRegex”的构造函数

现在代码是:

private List<SyntaxRegex> _syntaxRegexList = new List<SyntaxRegex>();
public class SyntaxRegex
{
   public string title;
   public string regex;
   public Color color;
}
Public List<SyntaxRegex> SyntaxRegexList
{
   get{_syntaxRegexList = value;}
   set{return _regexList;}
}
private List\u syntaxRegexList=new List();
公共类综合征
{
公共字符串标题;
公共字符串正则表达式;
公共色彩;
}
公共列表SyntaxRegexList
{
获取{u syntaxRegexList=value;}
set{return\u regexList;}
}

您需要添加类型转换器/编辑器;一个良好的开端是添加:

[TypeConverter(typeof(ExpandableObjectConverter))]
在每个
类的上方
定义。例如,以下方法可以很好地工作(注意,我更改为属性,删除了列表设置器,等等):

[TypeConverter(typeof(ExpandableObjectConverter))]
福班{
私有列表_syntaxRegexList=新列表();
[TypeConverter(typeof(ExpandableObjectConverter))]
公共类综合征
{
公共重写字符串ToString(){
返回字符串.IsNullOrEmpty(标题)?“(无标题)”:标题;
}
公共字符串标题{get;set;}
公共字符串正则表达式{get;set;}
公共颜色{get;set;}
}
[显示名称(“模式”)]
公共列表SyntaxRegexList
{
获取{return\u syntaxRegexList;}
}
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.Run(新表单
{
控制=
{
新属性网格{
Dock=DockStyle.Fill,
SelectedObject=newfoo()
}
}
});
}
}

具体的错误消息还让我怀疑您的实际类型是否是public,是否有一个公共的无参数构造函数(它没有编译的事实使我怀疑您没有发布实际代码…

我缩短了代码的长度以节省空间。添加类型转换器成功了。@Joshua-缩短空间是可以的,但是缩短的版本最好仍然是编译器(*),并演示问题*=除非您询问阻止编译的语法问题。
[TypeConverter(typeof(ExpandableObjectConverter))]
class Foo {
    private List<SyntaxRegex> _syntaxRegexList = new List<SyntaxRegex>();
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class SyntaxRegex
    {
       public override string ToString() {
           return string.IsNullOrEmpty(Title) ? "(no title)" : Title;
       }
       public string Title { get; set; }
       public string Regex { get; set; }
       public Color Color { get; set; }
    }
    [DisplayName("Patterns")]
    public List<SyntaxRegex> SyntaxRegexList
    {
        get { return _syntaxRegexList; }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form
        {
            Controls =
            {
                new PropertyGrid { 
                    Dock = DockStyle.Fill,
                    SelectedObject = new Foo()
                }
            }
        });
    }
}