PropertyGrid显示也是类的类成员

时间:2011-03-10 08:41:47

标签: c# propertygrid

我将类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中的Test-member那样展示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中列出。它必须是getter和setter的属性。那就是解决方案。所以上面的片段解决了这个问题。无论如何,谢谢你的回复:)。

2 个答案:

答案 0 :(得分:4)

<强>错误:

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

答案 1 :(得分:2)

抱歉,我误解了这个问题。

您可以在这些链接中找到更多详细信息

  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
  4. 希望有所帮助:)

    更新:

    我从here

    复制了代码

    并按此修改。

    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;
    

    它为我工作。

    Property Grid

    <击> 对于不希望在属性网格中显示的属性,将Browsable设为false。

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

    <击>