在 PropertyGrid 中实现子字段

发布于 2024-09-02 17:18:43 字数 587 浏览 5 评论 0原文

好吧,我的 C# 术语不太好,所以我将尝试用一个小例子来解释这一点。如果您创建一个在 PropertyGrid 中使用的类,并且您具有以下值:

class Test
{
    public Point example { get; set; }
}

这将生成一个 PropertyGrid,它具有一个可扩展对象“example”,该对象具有字段 X 和 Y 以创建“Point”。

我正在尝试创建一个对象“name”,其中包含字段“firstname”和“lastname”,所以我有:

class Test
{
    public Name example { get; set; }
}

public struct Name
{
    public string firstname { get; set; }
    public string lastname { get; set; }
}

但这并没有按预期工作。

我认为我需要重写一些方法才能使其正常工作,但是由于我实际上没有 PropertyGrid 的术语,因此我很难找到解决方案。

任何帮助都会很棒。

Alright so my terminology when it comes to C# isn't great, so I'll attempt to explain this with a small example. If you create a class which you are using within a PropertyGrid and you have the following values:

class Test
{
    public Point example { get; set; }
}

This will produce a PropertyGrid which has an expandable object "example" which has fields X and Y in order to create a "Point".

I'm attempting to create an object "name" which has fields "firstname" and "lastname", so I have:

class Test
{
    public Name example { get; set; }
}

public struct Name
{
    public string firstname { get; set; }
    public string lastname { get; set; }
}

This however isn't working as intended.

I think I need to override some method(s) in order to get this working, however since I don't really have the terminology down for PropertyGrids it is difficult for me to find a solution.

Any help would be great.

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

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

发布评论

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

评论(2

蝶舞 2024-09-09 17:18:43

经过大量查找后,我终于弄清楚了,缺少的关键字是“ExpandableObjectConverter”。

不管怎样,这里是示例代码:

public Form1()
{
    InitializeComponent();

    Person x = new Person();
    propertyGrid1.SelectedObject = x;
}

public class Person
{
    public string Caption { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }

    private Name _name = new Name();

    public Name testName
    {
        get { return _name; }
    }
}

PropertyGrids 确实令人困惑。

After a lot of looking around I finally was able to figure it out, the missing keyword was "ExpandableObjectConverter."

Anyway, here is example code:

public Form1()
{
    InitializeComponent();

    Person x = new Person();
    propertyGrid1.SelectedObject = x;
}

public class Person
{
    public string Caption { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }

    private Name _name = new Name();

    public Name testName
    {
        get { return _name; }
    }
}

PropertyGrids sure are confusing.

初见终念 2024-09-09 17:18:43

我相信在这种情况下,自动实现的属性定义

   public string Name { get; set; }

不适用于结构类型,但适用于类类型。在第一个示例中,Point 是一个类类型。

尝试

class Name
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
}

一下

class Test
{
  public Name example {get; set;}
}

可能会起作用。

I believe that in this instance auto-Implimented property definitions

   public string Name { get; set; }

don't work for struct types but class types. In the first example Point is a class type.

Try

class Name
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
}

then

class Test
{
  public Name example {get; set;}
}

may work.

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