首页  :: 新随笔  :: 联系 :: 管理

文章转载于http://www.cnblogs.com/guanjinke/archive/2006/12/11/589372.html
   上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。
 现在在前一篇文章中的创建的控件代码中添加一个Scope属性:

1 Browsable(true)]
2 public Scope Scope
3 {
4 get
5 {
6 return _scope;
7 }
8 set
9 {
10 _scope = value;
11 }
12 }

这个属性的类型是Scope类,代码如下:

   添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:
现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:

1 public class Scope
2 {
3 private Int32 _min;
4 private Int32 _max;
5
6 public Scope()
7 {
8 }
9
10  public Scope(Int32 min, Int32 max)
11 {
12 _min = min;
13 _max = max;
14 }
15
16 [Browsable(true)]
17 public Int32 Min
18 {
19 get
20 {
21 return _min;
22 }
23 set
24 {
25 _min = value;
26 }
27 }
28
29 [Browsable(true)]
30 public Int32 Max
31 {
32 get
33 {
34 return _max;
35 }
36 set
37 {
38 _max = value;
39 }
40
41 }
42 }
1 public class ScopeConverter : TypeConverter
2 {
3 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
4 {
5 if (sourceType == typeof(String)) return true;
6
7 return base.CanConvertFrom(context, sourceType);
8 }
9
10 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
11 {
12 if (destinationType == typeof(String)) return true;
13
14 if (destinationType == typeof(InstanceDescriptor)) return true;
15
16 return base.CanConvertTo(context, destinationType);
17 }
18
19 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
20 {
21 String result = "";
22 if (destinationType == typeof(String))
23 {
24 Scope scope = (Scope)value;
25 result = scope.Min.ToString()+"," + scope.Max.ToString();
26 return result;
27
28 }
29
30 if (destinationType == typeof(InstanceDescriptor))
31 {
32 ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[] {typeof(Int32),typeof(Int32) });
33 Scope scope = (Scope)value;
34 return new InstanceDescriptor(ci, new object[] { scope.Min,scope.Max });
35 }
36 return base.ConvertTo(context, culture, value, destinationType);
37 }
38
39 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
40 {
41 if (value is string)
42 {
43 String[] v = ((String)value).Split(',');
44 if (v.GetLength(0) != 2)
45 {
46 throw new ArgumentException("Invalid parameter format");
47 }
48
49 Scope csf = new Scope();
50 csf.Min = Convert.ToInt32(v[0]);
51 csf.Max = Convert.ToInt32(v[1]);
52 return csf;
53 }
54 return base.ConvertFrom(context, culture, value);
55 }
56 }
1 [TypeConverter(typeof(ScopeConverter))]
2 public class Scope

添加完以后build工程,然后切换到测试工程,选中控件,在属性浏览器里查看属性,现在的Scope属性可以编辑了,如下图所示: 
 
我们修改默认的值,然后看看Form设计器为我们生成了什么代码:

1 this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
2 this.myListControl1.Item.Add(1);
3 this.myListControl1.Item.Add(2);
4 this.myListControl1.Item.Add(3);
5 this.myListControl1.Item.Add(6);
6 this.myListControl1.Item.Add(8);
7 this.myListControl1.Item.Add(9);
8 this.myListControl1.Location = new System.Drawing.Point(12, 34);
9 this.myListControl1.Name = "myListControl1";
10 this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);
11 this.myListControl1.Size = new System.Drawing.Size(220, 180);
12 this.myListControl1.TabIndex = 1;
13 this.myListControl1.Text = "myListControl1";

   关键是这一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope类的类型转换器为属性提供了实例化的代码。