gpt4 book ai didi

c# - PropertyGrid 显示也是类的类成员

转载 作者:太空宇宙 更新时间:2023-11-03 19:30:34 28 4
gpt4 key购买 nike

我有 类 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前面有一个expandable-icon但是不能展开。我错过了什么?明确一点:我可以显示 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";
}
}

但我确实解决了这个问题(巧合)。公共(public)变量似乎没有列在 propertygrid 中。它必须是具有 getter 和 setter 的属性。这就是解决方案。所以上面的代码片段解决了这个问题。无论如何感谢您的回复:)。

最佳答案

错误:

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

关于c# - PropertyGrid 显示也是类的类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5257217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com