我有類PGMain作爲SelectedObject PropertyGrid中: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
}
現在我想擴大的TestClass PropertyGrid中的成員。所以,我試過如下:
[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";
}
}
但我確實解決問題(巧合)。看來公共變量沒有在propertygrid中列出。它必須是獲得者和制定者的屬性。這是解決方案。所以上面的代碼解決了這個問題。無論如何感謝您的回覆:)。
您需要定義的C#的屬性,而不是字段。屬性網格看不到字段。 – 2011-03-10 12:58:39