PropertyGrid 显示也是类的类成员

发布于 2024-10-21 12:10:14 字数 2474 浏览 7 评论 0原文

我有 PGMain 类作为 propertygrid 中的 SelectedObject

         [DefaultPropertyAttribute("Basic")]
[Serializable]
public class PGMain
{    
    private TestClass m_TEST = new TestClass();
    [CategoryAttribute("TEST")]
    public TestClass TEST
    {
        get { return m_TEST; }
        set { m_TEST = value; }
}
    // More members are here
}

现在我想在 PropertyGrid 中扩展 TestClass 的成员。所以我尝试了以下方法:

   [Serializable]
[DescriptionAttribute("Expand to see the options for the application.")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TestClass : ExpandableObjectConverter
{
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]        
    public string Name = "";
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public object Value = null;
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public bool Include = true;

    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
    {
        if (destinationType == typeof(TestClass))
            return true;

        return base.CanConvertTo(context, destinationType);
    }
}

结果是propertygrid中的TestClass前面有一个可扩展的图标,但无法扩展。我缺少什么? 需要明确的是:我可以显示 PGMain 类的可扩展成员,但不能显示 PGMain 类成员的可扩展成员,例如 PGMain 中的测试成员。


编辑:

不,我有 2 个类,而不是 1 个。

  [DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooA
{
    private fooB m_TestMember = new fooB();
    [Browsable(true)]
    [CategoryAttribute("Test category"), DescriptionAttribute("desctiption here")] // <<<<< this one works.
    [TypeConverter(typeof(fooB))]
    public fooB TestMember
    {
        get { return m_TestMember; }
        set { m_TestMember = value; }
    }

}

[DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooB
{
    private string m_ShowThisMemberInGrid = "it works"; // <<<<< this doesn NOT work
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string ShowThisMemberInGrid
    {
        get { return m_ShowThisMemberInGrid; }
        set { m_ShowThisMemberInGrid = value; }
    }

    public override string ToString()
    {
        return "foo B";
    }
}

但我确实解决了问题(巧合)。公共变量似乎未在属性网格中列出。它必须是具有 getter 和 setter 的属性。这就是解决方案。所以上面的代码片段解决了这个问题。不管怎样,谢谢你的回复:)。

I have the class PGMain as the SelectedObject in the propertygrid:

         [DefaultPropertyAttribute("Basic")]
[Serializable]
public class PGMain
{    
    private TestClass m_TEST = new TestClass();
    [CategoryAttribute("TEST")]
    public TestClass TEST
    {
        get { return m_TEST; }
        set { m_TEST = value; }
}
    // More members are here
}

Now I would like to expand the members of the TestClass in the PropertyGrid. So I tried the following:

   [Serializable]
[DescriptionAttribute("Expand to see the options for the application.")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TestClass : ExpandableObjectConverter
{
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]        
    public string Name = "";
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public object Value = null;
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public bool Include = true;

    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
    {
        if (destinationType == typeof(TestClass))
            return true;

        return base.CanConvertTo(context, destinationType);
    }
}

The result is that there is an expandable-icon in front of the TestClass in the propertygrid but it can not be expanded. What am I missing?
Just to be clear: I can show expandable members of the PGMain class but NOT expandable members of the members of the PGMain class like the Test-member in PGMain.


Edit:

No I have 2 classes NOT 1.

  [DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooA
{
    private fooB m_TestMember = new fooB();
    [Browsable(true)]
    [CategoryAttribute("Test category"), DescriptionAttribute("desctiption here")] // <<<<< this one works.
    [TypeConverter(typeof(fooB))]
    public fooB TestMember
    {
        get { return m_TestMember; }
        set { m_TestMember = value; }
    }

}

[DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooB
{
    private string m_ShowThisMemberInGrid = "it works"; // <<<<< this doesn NOT work
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string ShowThisMemberInGrid
    {
        get { return m_ShowThisMemberInGrid; }
        set { m_ShowThisMemberInGrid = value; }
    }

    public override string ToString()
    {
        return "foo B";
    }
}

But I did solve the problem (by coincidence). It appears that public variables are not listed in the propertygrid. It HAVE to be properties with getters and setters. That was the solution. So the above snippet solved the problem. Thanks for your replies anyway :).

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

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

发布评论

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

评论(2

注定孤独终老 2024-10-28 12:10:14

错误:

[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string Name = "";

好:

    private string m_Name = new string();
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }

Wrong:

[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string Name = "";

Good:

    private string m_Name = new string();
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }
不打扰别人 2024-10-28 12:10:14

抱歉我误解了这个问题。

您可以在这些链接 http://msdn.microsoft 上找到更多详细信息

  1. 。 com/en-us/library/aa302326.aspx#usingpropgrid_topic6a
  2. http:// /www.codeproject.com/KB/miscctrl/bending_property.aspx
  3. http://msdn.microsoft.com/en-us/library/aa302334.aspx

希望它有帮助:)

更新:

我从 这里

并像这样修改。

public class SamplePerson
{
    public string Name
    {
        get;
        set;
    }
    public Person Person
    {
        get;
        set;
    }
}

以某种形式,我做了类似的事情

SamplePerson h = new SamplePerson();
h.Person = new Person
{
    Age = 20,
    FirstName = "f",
    LastName = "l"
};
this.propertyGrid1.SelectedObject = h;

,它对我有用。

属性网格


对于您不想在属性网格中显示的属性,将 Browsable 设置为 false。

[Browsable(false)]
public bool Include
{
get;set;
}

Sorry I misinterpret the question.

You can find more details on these links

  1. http://msdn.microsoft.com/en-us/library/aa302326.aspx#usingpropgrid_topic6a
  2. http://www.codeproject.com/KB/miscctrl/bending_property.aspx
  3. http://msdn.microsoft.com/en-us/library/aa302334.aspx

Hope it helps :)

UPDATE:

I copied the code from here

And modified like this.

public class SamplePerson
{
    public string Name
    {
        get;
        set;
    }
    public Person Person
    {
        get;
        set;
    }
}

And in the form I have done something like

SamplePerson h = new SamplePerson();
h.Person = new Person
{
    Age = 20,
    FirstName = "f",
    LastName = "l"
};
this.propertyGrid1.SelectedObject = h;

And its working for me.

Property Grid


Provide Browsable as false for properties you don't want to display in the property grid.

[Browsable(false)]
public bool Include
{
get;set;
}

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