2014-11-23 28 views
2

我一直在PropertyGrids玩耍,它们链接到类,我试图找出如何(如果可能)我可以显示一个类,与PropertyGrid的内部子类在结构(好像他们都在一个班)PropertyGrid中有多个嵌套类

我有几个类,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类,但只是一行,并且无法编辑名称属性。

如果我像上面那样使用[TypeConverter(typeof(ExpandableObjectConverter))],那么我可以看到一个Bar的扩展箭头,并且在Name下面的属性...似乎都很好。

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

如果我使用[类别(“酒吧”),然后将其包括在内,并根据需要在平坦的结构组织,但仍存在上述问题(扩大水平和类名)

我错过在这里很明显吗?或者是我需要实现某种自定义类型转换器的情况?如果是这样,怎么去做呢?

编辑:

为了进一步澄清,当我使用嵌套类,它显示像下面,所以显示在右边的类名,并展开箭头。

enter image description here

我想有嵌套类,如如下图所示/仿佛富只是一个类代码和名称。

enter image description here

EDIT2:

试图执行一个类型转换器,这似乎显示正常,但现在我不能在所有(即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:

类型CONV erter似乎为获取值而工作,但现在无法编辑栏。名称值,它仅仅是灰色的:

enter image description here

当前的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

我觉得这是在太硬篮下去。太多的时间转圈圈:(

+0

我想你真正想要的是一个自定义类型编辑器。参见[用户界面类型编辑](http://msdn.microsoft.com/en-us/library/ms171838.aspx) – 2014-11-23 20:12:53

+0

谢谢,我正在尝试fi现在出来了。 – user3295596 2014-11-24 12:50:38

回答

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