零.引言
PropertyGrid用來顯示某一對象的屬性,但是並不是所有的屬性都能編輯,基本數據類型(int, double等)和.Net一些封裝的類型(Size,Color等)可以編輯,但是對於自己定義的類型屬性,是不能編輯的,本文主要講述如何為自定義類型作為屬性時,在PropertyGrid中進行編輯,以及進行設計時序列化,本文主要參考MSDN,錯誤和不足之處還望指正。
一.自定義類屬性
在PropertyGrid中能夠編輯的都是.Net中自己封裝的類,如果在一個類中有一個屬性是我們自己定義的類型,在PropertyGrid中會是怎樣的呢?看下面這個例子:
假如現在有一個類Line:

1 public class Line 2 { 3 Point P1; 4 Point P2; 5 6 public Point Point1 7 { 8 get{return P1;} 9 set{P1 = value;} 10 } 11 public Point Point2 12 { 13 get{return P2;} 14 set{P2 = value;} 15 } 16 public Line(Point point1, Point point2) 17 { 18 P1 = point1; 19 P2 = point2; 20 } 21 }
有一個控件類包含一個Line類型的屬性:

1 public class MyControl : System.Windows.Forms.UserControl 2 { 3 Line _line; 4 5 public MyControl() 6 { 7 _line = new Line(new Point(0, 0),new Point(100, 100)); 8 } 9 10 public Line MyLine 11 { 12 get{return _line;} 13 set{_line = value;} 14 } 15 16 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 17 { 18 e.Graphics.DrawLine(Pens.Red, this._line.Point1, this._line.Point2); 19 base.OnPaint(e); 20 } 21 }
重新生成,從工具箱中將該控件拖入Form中,查看他的屬性,在PropertyGrid中顯示如下:
可見MyLine屬性的值不顯示,且是不能編輯的。這是因為PropertyGrid根本不知道Line是個什么類型,不知道要怎么顯示,如果要其能在PropertyGrid中顯示,必須給他提供轉換器。
二.轉換器概念
PropertyGrid中屬性的值都是以字符串的形式呈現給我們看的,顯示一個對象的屬性時,要將對象的屬性值轉換為字符串顯示出來,而設置屬性時,要將字符串轉換為對象的屬性值。這就需要一個轉換器。在.Net中定義了一個TypeConverter 類,用來作為這些轉換器的基類。.Net為一些類設計了專門的轉換類,如:System.Drawing.ColorConverter ,System.Drawing.FontConverter等等,(具體參見MSDN中TypeConverter的繼承關系)因此在PropertyGrid中能直接編輯這些屬性。我們自己定義的類沒有這樣的類型轉換器,因此在PropertyGrid中無法編輯,需要設計自己的轉換器。
先來看一下MSDN中對TypeConverter的描述:TypeConverter類提供一種將值的類型轉換為其他類型以及訪問標准值和子屬性的統一方法。
繼承者說明:
從 TypeConverter 繼承,以實現您自己的轉換要求。當從類繼承時,可以重寫以下方法:
- 若要支持自定義類型轉換,請重寫 CanConvertFrom、CanConvertTo、ConvertFrom 和 ConvertTo 方法。
- 若要轉換必須重新創建對象才能更改其值的類型,請重寫 CreateInstance 和 GetCreateInstanceSupported 方法。
- 若要轉換支持屬性 (Property) 的類型,請重寫 GetProperties 和 GetPropertiesSupported 方法。如果轉換的類沒有屬性 (Property),而您需要實現屬性 (Property),則可以將 TypeConverter.SimplePropertyDescriptor 類用作實現屬性 (Property) 說明符的基。當從 TypeConverter.SimplePropertyDescriptor 繼承時,必須重寫 GetValue 和 SetValue 方法。
- 若要轉換支持標准值的類型,請重寫 GetStandardValues、GetStandardValuesExclusive、GetStandardValuesSupported 和 IsValid 方法。
三.添加轉換器
好了,了解了基本原理后,我們來為Line添加轉換器。這里新建一個LineConverter類,並繼承於TypeConverter。

1 public class LineConverter : TypeConverter 2 { 3 //該方法判斷此類型可以轉換為哪些類型 4 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 5 { 6 if (destinationType == typeof(string)) 7 { 8 return true; 9 } 10 11 if (destinationType == typeof(InstanceDescriptor)) 12 { 13 return true; 14 } 15 16 //調用基類方法處理其他情況 17 return base.CanConvertTo(context, destinationType); 18 } 19 //該方法判斷哪些類型可以轉換為此類型 20 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 21 { 22 if (sourceType == typeof(string)) 23 { 24 return true; 25 } 26 27 return base.CanConvertFrom(context, sourceType); 28 } 29 30 // 將該類型轉換為字符串和InstanceDescriptor 31 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 32 { 33 if (destinationType == typeof(string) && value != null) 34 { 35 Line t = (Line)value; 36 string str = t.Point1.X + "," + t.Point1.Y + "," + t.Point2.X + "," + t.Point2.Y; 37 return str; 38 } 39 40 if (destinationType == typeof(InstanceDescriptor) && value != null) 41 { 42 ConstructorInfo ci = typeof(TestTypeConverter.Line).GetConstructor(new Type[] { typeof(Point), typeof(Point) }); 43 Line t = (Line)value; 44 return new InstanceDescriptor(ci, new object[] { t.Point1, t.Point2 }); 45 } 46 47 return base.ConvertTo(context, culture, value, destinationType); 48 } 49 //字符串和InstanceDescriptor轉換為該類 50 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 51 { 52 if (value is string) 53 { 54 string str = (string)value; 55 str = str.Trim(); 56 string[] v = str.Split(','); 57 if (v.Length != 4) 58 { 59 throw new NotSupportedException("Invalid parameter format"); 60 } 61 62 int x1 = 0; 63 int y1 = 0; 64 int x2 = 0; 65 int y2 = 0; 66 bool res = int.TryParse(v[0], out x1); 67 if (res == false) throw new NotSupportedException("Invalid parameter format"); 68 res = int.TryParse(v[1], out y1); 69 if (res == false) throw new NotSupportedException("Invalid parameter format"); 70 res = int.TryParse(v[2], out x2); 71 if (res == false) throw new NotSupportedException("Invalid parameter format"); 72 res = int.TryParse(v[3], out y2); 73 if (res == false) throw new NotSupportedException("Invalid parameter format"); 74 75 Line line = new Line(new Point(x1, y1), new Point(x2, y2)); 76 return line; 77 } 78 79 return base.ConvertFrom(context, culture, value); 80 } 81 }
在轉換器類中,我們重寫了四個函數,也就是在繼承者說明中第一條的四個函數:
① CanConvertTo:用來說明此類可以轉換為哪些類型,能轉換就返回true,這里我們讓他能轉換為string和InstanceDescriptor,InstanceDescriptor是存儲描述對象實例的信息,這些信息可用於創建對象的實例。一般轉換都要實現這個轉換,后面進行說明。
② CanConvertFrom:說明該類能有什么類型轉換過來,能轉換返回true,這里只對string類型進行轉換。
③ ConvertTo:具體的轉換實現,也就是提供方法將該類轉換為在CanConvertTo中確定可以轉換的類型。這里實現string和InstanceDescriptor的轉換。
④ ConvertFrom:具體的轉換實現,也就是提供方法將該類轉換為在CanConvertFrom中確定可以轉換的類型。這里實現string的轉換。
注意,每個方法處理完自己的轉換后,依然要調用基類的函數來處理其他的情況。
重寫這四個方法后,給Line類型加上TypeConverter特性,在Line類定義的上面加上[TypeConverter(typeof(LineConverter))]。這樣在需要轉換的地方,就會調用我們所寫的方法進行轉換,Line屬性在PropertyGrid中顯示時,會調用ConvertTo,轉換為字符串輸出;設置屬性時,會調用ConvertFrom將字符串轉換為屬性值,當然,字符串必須要有一定的格式,這就需要在轉換的過程中進行判斷,見ConvertFrom方法。
重新生成,現在看MyControl中MyLine屬性在PropertyGrid中的顯示:
可以看到MyLine屬性顯示出來了(以x1,y1,x2,y2的格式,我們在ConverTo方法中指定的),並可以編輯了,但必須是x1,y1,x2,y2的格式,否則會出現如下錯誤提示:
這是我們在ConvertFrom方法中進行判斷的。
四.編輯復雜屬性的子屬性
現在可以編輯屬性了,但是必須使用x1,y1,x2,y2固定的格式,不方便,而且容易出錯,可不可以分別設置Line的兩個點呢,可以,這里就需要編輯子屬性了。Line中有Point1和Point2兩個屬性,我們讓他們也顯示出來。
這里就是繼承者說明中的第三條了。重寫 GetProperties 和 GetPropertiesSupported 方法。在LineConverter中重寫這兩個方法:

1 public override bool GetPropertiesSupported(ITypeDescriptorContext context) 2 { 3 return true; 4 //return base.GetPropertiesSupported(context); 5 } 6 7 public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 8 { 9 return TypeDescriptor.GetProperties(value, attributes); 10 //return base.GetProperties(context, value, attributes); 11 } 12
重新生成,現在看MyControl中MyLine屬性在PropertyGrid中的顯示:
可以看到現在我們可以編輯MyLine的子屬性了。
五.屬性的設計時串行化
最后說一說ConvertTo方法中為什么要實現InstanceDescriptor的轉換。Visual Studio在我們編輯控件時,會在Form1.Designer.cs文件中自動的生成代碼,設置控件的屬性,這叫屬性對設計時序列化,如下:
我們定義的MyLine屬性,在這里並沒有,如何使它出現在這里呢,只需給MyLine屬性加上DesignerSerializationVisibility特性。如下

1 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 2 public Line MyLine 3 { 4 get 5 { 6 return _line; 7 } 8 set 9 { 10 _line = value; 11 } 12 } 13
這里DesignerSerializationVisibility.Visible表明代碼生成器生成對象的代碼。DesignerSerializationVisibility.Content說明該屬性在編輯時要代碼生成器產生對象內容的代碼,而不是對象本身的代碼。DesignerSerializationVisibility.Hide代碼生成器不生成對象的代碼。
重新生成,並改變控件的位置,打開Form1.Designer.cs,會發現自動生成了MyLine屬性的設置值。
這是如何生成的呢,關鍵就在ConvertTo方法中實現InstanceDescriptor的轉換,該方法告訴代碼生成器,如何去構造一個Line類型,生成時,調用Line的構造函數構造新對象初始化MyLine屬性值。
六.總體的代碼
下面是完整的代碼:

1 using System; 2 using System.ComponentModel; 3 using System.ComponentModel.Design.Serialization; 4 using System.Drawing; 5 using System.Globalization; 6 using System.Reflection; 7 8 namespace TestTypeConverter 9 { 10 //線條類 11 [TypeConverter(typeof(LineConverter))] 12 public class Line 13 { 14 // Line members. 15 Point P1; 16 Point P2; 17 18 public Point Point1 19 { 20 get 21 { 22 return P1; 23 } 24 set 25 { 26 P1 = value; 27 } 28 } 29 30 public Point Point2 31 { 32 get 33 { 34 return P2; 35 } 36 set 37 { 38 P2 = value; 39 } 40 } 41 42 public Line(Point point1, Point point2) 43 { 44 P1 = point1; 45 P2 = point2; 46 } 47 } 48 49 //轉換器類 50 public class LineConverter : TypeConverter 51 { 52 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 53 { 54 if (destinationType == typeof(string)) 55 { 56 return true; 57 } 58 59 if (destinationType == typeof(InstanceDescriptor)) 60 { 61 return true; 62 } 63 return base.CanConvertTo(context, destinationType); 64 } 65 66 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 67 { 68 if (sourceType == typeof(string)) 69 { 70 return true; 71 } 72 73 return base.CanConvertFrom(context, sourceType); 74 } 75 76 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 77 { 78 if (destinationType == typeof(string) && value != null) 79 { 80 Line t = (Line)value; 81 string str = t.Point1.X + "," + t.Point1.Y + "," + t.Point2.X + "," + t.Point2.Y; 82 return str; 83 } 84 85 if (destinationType == typeof(InstanceDescriptor)) 86 { 87 ConstructorInfo ci = typeof(TestTypeConverter.Line).GetConstructor(new Type[] { typeof(Point), typeof(Point) }); 88 Line t = (Line)value; 89 return new InstanceDescriptor(ci, new object[] { t.Point1, t.Point2 }); 90 } 91 return base.ConvertTo(context, culture, value, destinationType); 92 } 93 94 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 95 { 96 if (value is string) 97 { 98 string str = (string)value; 99 str = str.Trim(); 100 string[] v = str.Split(','); 101 if (v.Length != 4) 102 { 103 throw new NotSupportedException("Invalid parameter format"); 104 } 105 106 int x1 = 0; 107 int y1 = 0; 108 int x2 = 0; 109 int y2 = 0; 110 bool res = int.TryParse(v[0], out x1); 111 if (res == false) throw new NotSupportedException("Invalid parameter format"); 112 res = int.TryParse(v[1], out y1); 113 if (res == false) throw new NotSupportedException("Invalid parameter format"); 114 res = int.TryParse(v[2], out x2); 115 if (res == false) throw new NotSupportedException("Invalid parameter format"); 116 res = int.TryParse(v[3], out y2); 117 if (res == false) throw new NotSupportedException("Invalid parameter format"); 118 119 Line line = new Line(new Point(x1, y1), new Point(x2, y2)); 120 return line; 121 } 122 123 return base.ConvertFrom(context, culture, value); 124 } 125 126 127 public override bool GetPropertiesSupported(ITypeDescriptorContext context) 128 { 129 return true; 130 //return base.GetPropertiesSupported(context); 131 } 132 133 public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 134 { 135 return TypeDescriptor.GetProperties(value, attributes); 136 //return base.GetProperties(context, value, attributes); 137 } 138 } 139 140 //控件類 141 public class MyControl : System.Windows.Forms.UserControl 142 { 143 Line _line; 144 145 public MyControl() 146 { 147 _line = new TestTypeConverter.Line( 148 new Point(0, 0), 149 new Point(100, 100) 150 ); 151 } 152 153 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 154 public Line MyLine 155 { 156 get 157 { 158 return _line; 159 } 160 set 161 { 162 _line = value; 163 } 164 } 165 166 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 167 { 168 e.Graphics.DrawLine(Pens.Red, this._line.Point1, this._line.Point2); 169 base.OnPaint(e); 170 } 171 } 172 }
新建一個Windows工程,添加該文件,在工具箱中找到我們的MyControl控件,拖入Form中,在屬性框中查看控件的屬性。