2014-11-23 198 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"; 
    } 
}