具有多个嵌套类的Propertygrid

时间:2014-11-23 11:10:53

标签: c# class nested propertygrid

我一直在玩PropertyGrids,将它们连接到类,并试图弄清楚如何(如果可能)我可以显示一个类,在平面结构(好像它们都在一个类中)

我有几个课程,Foo和Bar如下:

[Serializable()]
public class Foo
{
    private string m_Code;
    private Bar m_Bar = new Bar();

    [DisplayName("Code")]
    [Description("The Code of Foo")]
    public string Code
    {
        get { return m_Code; }
        set { m_Code = value; }
    }

    public Bar Bar
    {
        get { return m_Bar; }
        set { m_Bar = value; }
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
[Serializable()]
public class Bar
{
    private string m_Name;

    [DisplayName("Name")]
    [Description("The Name of Bar")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }
}

我想要了解/弄清楚我是否可以修改数据随后在PropertyGrid中显示的方式。

具体来说,我想在平面/有组织的结构中显示Foo.Code和Bar.Name,就像它们都在同一个类中一样。

如果我使用不使用TypeConverter,或尝试使用[TypeConverter(typeof(Bar))]那么我在PropertyGrid中看到我的Bar类,但只是一行,并且无法编辑Name属性

如果我如上所述使用[TypeConverter(typeof(ExpandableObjectConverter))],那么我可以看到Bar的展开箭头,在其下面,Name ...的属性似乎都很好。

但是,我必须扩展组,并在右侧显示类名。显然我可以调用PropertyGrid.ExpandAllGridItems();根据需要,但它也很难看。

如果我使用[Category(“Bar”)],那么它会被包含在内,并根据需要在平面结构中组织,但仍然存在上述问题(扩展级别和类名)

我错过了一些明显的东西吗?或者是我需要实现某种自定义类型转换器的情况?如果是这样,该如何去做呢?

修改

为了进一步说明,当我使用嵌套类时,它显示如下,所以在右边显示类名,然后展开箭头。

enter image description here

我希望嵌套类如下所示/好像Foo只是一个包含Code和Name的类。

enter image description here

EDIT2:

尝试实现一个Type转换器,这似乎显示正常,但现在我根本无法编辑该值(即Bar.Name)

[TypeConverter(typeof(BarConverter))]
[Serializable()]
public class Bar
{
    private string m_Name;

    [DisplayName("Name")]
    [Description("The Name of Bar")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }
}

public class BarConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        Bar _Bar = (Bar)value;
        return _Bar.Name;
    }

}

编辑3:

类型转换器似乎适用于获取值,但现在无法编辑Bar.Name值,它只是变灰了:

enter image description here

Current TypeConverter

public class BarConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        Bar _Bar = (Bar)value;
        return _Bar.Name;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        return base.ConvertFrom(context, culture, value);
    }
}

编辑#4

我认为这是在太难的篮子里。太多时间绕圈:(

2 个答案:

答案 0 :(得分:0)

我认为你应该覆盖ExpandableObjectConverter而不是TypeConverter

public class BarConverter : ExpandableObjectConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
             Bar _Bar = (Bar) value;
             return _Bar.Name;
        }
    }

答案 1 :(得分:0)

我通过覆盖类的ToString()方法解决了类似的问题。找到解决方案here

[Serializable()]
public class Bar
{
    private string m_Name;

    [DisplayName("Name")]
    [Description("The Name of Bar")]
    public string Name
    {
        get { return m_Name; }
    }
    public override string ToString()
    {
        return "Name you want to display";
    }
}
相关问题