上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。
现在在前一篇文章中的创建的控件代码中添加一个Scope属性:
这个属性的类型是Scope类,代码如下:
添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:

public class ScopeConverter : TypeConverter

{

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

{

if (sourceType == typeof(String)) return true;

return base.CanConvertFrom(context, sourceType);

}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)

{

if (destinationType == typeof(String)) return true;

if (destinationType == typeof(InstanceDescriptor)) return true;

return base.CanConvertTo(context, destinationType);

}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)

{

String result = "";

if (destinationType == typeof(String))

{

Scope scope = (Scope)value;

result = scope.Min.ToString()+"," + scope.Max.ToString();

return result;

}

if (destinationType == typeof(InstanceDescriptor))

{

ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[]

{typeof(Int32),typeof(Int32) });

Scope scope = (Scope)value;

return new InstanceDescriptor(ci, new object[]

{ scope.Min,scope.Max });

}

return base.ConvertTo(context, culture, value, destinationType);

}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

{

if (value is string)

{

String[] v = ((String)value).Split(',');

if (v.GetLength(0) != 2)

{

throw new ArgumentException("Invalid parameter format");

}

Scope csf = new Scope();

csf.Min = Convert.ToInt32(v[0]);

csf.Max = Convert.ToInt32(v[1]);

return csf;

}

return base.ConvertFrom(context, culture, value);

}

}
现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:

[TypeConverter(typeof(ScopeConverter))]

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

this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;

this.myListControl1.Item.Add(1);

this.myListControl1.Item.Add(2);

this.myListControl1.Item.Add(3);

this.myListControl1.Item.Add(6);

this.myListControl1.Item.Add(8);

this.myListControl1.Item.Add(9);

this.myListControl1.Location = new System.Drawing.Point(12, 34);

this.myListControl1.Name = "myListControl1";

this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);

this.myListControl1.Size = new System.Drawing.Size(220, 180);

this.myListControl1.TabIndex = 1;

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