PropertyGrid отображает членов класса, которые также являются классами - PullRequest
1 голос
/ 10 марта 2011

У меня есть класс PGMain в качестве 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
}

Теперь я хотел бы расширить члены 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);
    }
}

В результате перед значком 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";
    }
}

Но я решил проблему (по стечению обстоятельств).Похоже, что публичные переменные не перечислены в таблице свойств.Это должны быть свойства с геттерами и сеттерами.Это было решение.Таким образом, приведенный выше фрагмент решил проблему.В любом случае, спасибо за ваши ответы:).

Ответы [ 2 ]

4 голосов
/ 10 марта 2011

Неправильно:

[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; }
    }
2 голосов
/ 10 марта 2011

Извините, я неверно истолковал вопрос.

Более подробную информацию можно найти по этим ссылкам

  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

Надеюсь, это поможет:)

ОБНОВЛЕНИЕ:

Я скопировал код с здесь

Имодифицировано так.

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

Предоставьте Browseable как false для свойств, которые вы не хотите отображать в сетке свойств.

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

...