具有相同子节点和不同节点的 PropertyGrid

PropertyGrid with same childs and different nodes

我想要我的自定义 PropertyGrid,其中 student1 和 student2 作为节点,"Name,Section,Percentage,School" 作为两个节点的子节点。 我这样试过:

class StudentClass
{
    private string name;
    private string section;
    private string percentage;
    private string school;

    [CategoryAttribute("Student1")]
    public string School
    {
        get { return school; }
        set { school = value; }
    }
    [CategoryAttribute("Student1")]
    public string Percentage
    {
        get { return percentage; }
        set { percentage = value; }
    }
    [CategoryAttribute("Student1")]
    public string Section
    {
        get { return section; }
        set { section = value; }
    }
    [CategoryAttribute("Student1")]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string name1;
    [CategoryAttribute("Student2")]
    public string Name1
    {
        get { return name1; }
        set { name1 = value; }
    }
    private string section1;
    [CategoryAttribute("Student2")]
    public string Section1
    {
        get { return section1; }
        set { section1 = value; }
    }
    private string percentage1;
    [CategoryAttribute("Student2")]
    public string Percentage1
    {
        get { return percentage1; }
        set { percentage1 = value; }
    }
    private string school1;
    [CategoryAttribute("Student2")]
    public string School1
    {
        get { return school1; }
        set { school1 = value; }
    }
}

 public Form1()
    {
        InitializeComponent();
        StudentClass sc = new StudentClass();
        propertyGrid1.SelectedObject = sc1;
    }

输出如下图:

现在在上面的 Student2 图片中而不是 "Name1,Section1,Percentage1,School1" 我想显示与 student1 相同的图片。 但是我没有得到所需的输出。所以请帮我解决这个问题。 我在 VS2010

中使用 C# Winforms

并建议我如何拒绝调整列的大小,即我不应该允许用户调整列的大小。

您可以使用 DisplayName attribute:

private string name1;
[CategoryAttribute("Student2"), DisplayName("Name")]
public string Name1
{
    get { return name1; }
    set { name1 = value; }
}

但请注意,如果用户将 属性 网格设置为 A-Z 模式,它们最终将彼此相邻,无法真正区分它们。您可能会发现有更合适的方式来表示您的数据。