类列表的 C# 属性

发布于 2024-08-20 05:30:47 字数 593 浏览 6 评论 0原文

我正在尝试在 Visual Studio 设计器中使用属性网格。

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

这是我已经在代码中使用的一些示例代码。问题是,当开发人员进入设计模式时,他只能看到列表中有 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;}
}

I'm trying to use the property grid in the designer for Visual Studio.

I have a list of classes that I want the developer to be able to add to at design time so that the user can have access to extra features.

Here is some example code of what I have in the code already. The problem is when the developer goes to the design mode he can only see that there are x number of values in the list, but is unable to see any of the details. When trying to add a new item to the list the user is presented with an error.

Constructor on type 'EditorTextBox+SyntaxRegex' not found.

Now the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2024-08-27 05:30:47

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

[TypeConverter(typeof(ExpandableObjectConverter))]

在每个 class 定义上方添加:。例如,以下内容可以正常工作(请注意,我更改为属性,删除了列表设置器等):

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

具体的错误消息还让我想知道您的实际类型是否是带有公共无参数构造函数的公共类型(它无法编译的事实让我怀疑你还没有发布实际的代码......)

You need to add type converters / editors; a good start would be to add:

[TypeConverter(typeof(ExpandableObjectConverter))]

above each class definition. For example, the following works fine (note I changed to properties, removed the list setter, etc):

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

The specific error message also makes me wonder if your actual type is public with a public parameterless constructor (the fact that it doesn't compile makes me suspect you haven't posted the actual code...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文