2013-08-31 78 views
2

我將使用PropertyGrid顯示我的對象。這是info類。 Info類具有一些由類類型組成的屬性。但是,子類不顯示屬性。你有什麼主意嗎?如何使用PropertyGrid中的子類顯示對象

代碼段:

using System.ComponentModel; 
using System.Windows.Forms; 

namespace WindowsFormsApplication_propertyGrid 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      info _in = new info(); 
      this.propertyGrid1.SelectedObject = _in; 
     } 
    } 

    [DefaultPropertyAttribute("Name")] 
    [TypeConverter(typeof(ExpandableObjectConverter))] 
    public class info 
    { 
     private int _id; 
     [CategoryAttribute("Defaults")] 
     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     private string _name; 
     [CategoryAttribute("Defaults")] 
     public string Name 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     private DoublePoint _resultMarkPos; 
     [CategoryAttribute("Results")] 
     [TypeConverter(typeof(ExpandableObjectConverter))] 
     public DoublePoint ResultMarkPos 
     { 
      get { return _resultMarkPos; } 
      set { _resultMarkPos = value; } 
     } 

     public struct DoublePoint 
     { 
      public double x, y; 
     } 

     private subInfo1 _sub1; 
     [CategoryAttribute("SubInfo")] 
     [TypeConverter(typeof(ExpandableObjectConverter))] 
     public subInfo1 SubInfo1 
     { 
      get { return _sub1; } 
      set { _sub1 = value; } 
     } 

     private subInfo2 _sub2; 
     [CategoryAttribute("SubInfo2")] 
     [TypeConverter(typeof(ExpandableObjectConverter))] 
     public subInfo2 SubInfo2 
     { 
      get { return _sub2; } 
      set { _sub2 = value; } 
     } 

     public info() 
     { 
      this._id = 0; 
      this._name = "info"; 

      this._resultMarkPos.x = 0; 
      this._resultMarkPos.y = 0; 

      this._sub1 = new subInfo1 
      { 
       Id = 11, 
       Name = "sub11", 
      }; 

      this._sub2 = new subInfo2 
      { 
       Id = 22, 
       Name = "sub22", 
      }; 
     } 
    } 

    public class subInfo1 
    { 
     private int _id; 
     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     private string _name; 
     public string Name 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     public subInfo1() 
     { 
      this._id = 0; 
      this._name = "sub1"; 
     } 
    } 

    public class subInfo2 
    { 
     private int _id; 
     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     private string _name; 
     public string Name 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     public subInfo2() 
     { 
      this._id = 0; 
      this._name = "sub2"; 
     } 
    } 
} 

編輯 然而,結構的情況下不會對[的TypeConverter(typeof運算(ExpandableObjectConverter))]屬性影響。你有什麼主意嗎 ?

private DoublePoint _resultMarkPos; 
[CategoryAttribute("Results")] 
[TypeConverter(typeof(ExpandableObjectConverter))] 
public DoublePoint ResultMarkPos 
{ 
      get { return _resultMarkPos; } 
      set { _resultMarkPos = value; } 
} 

public struct DoublePoint 
{ 
     public double x, y; 
} 

回答

4

您需要使用的TypeConverter:

private subInfo1 _sub1;   
    [CategoryAttribute("SubInfo")] 
    [TypeConverter(typeof(ExpandableObjectConverter))] 
    public subInfo1 SubInfo1 
    { 
     get { return _sub1; } 
     set { _sub1 = value; } 
    } 
    private subInfo2 _sub2; 
    [CategoryAttribute("SubInfo2")] 
    [TypeConverter(typeof(ExpandableObjectConverter))] 
    public subInfo2 SubInfo2 
    { 
     get { return _sub2; } 
     set { _sub2 = value; } 
    } 
0

你的代碼是好的,但你忘了你的類聲明一個小小的額外的信息:public class MyClass : ExpandableObjectConverter

這裏是一個非常基本的使用類:

[TypeConverter(typeof(ExpandableObjectConverter))] 
public class info : ExpandableObjectConverter 
{ 
    private int _id; 

    public int Id 
    { 
     get { return _id; } 
     set { _id = value; } 
    } 

} 

一部分下次不要忘了...

相關問題