WinForm控件開發總結(五)-----為控件的復雜屬性提供類型轉換器
上一篇文章我已經介紹了TypeConverterAttribute元數據的作用,本文將通過代碼向你展示具體的實現。在這個例子中,我要給控件添加一個復雜的屬性,這個屬性對這個控件沒有什么功用,純粹是為了演示,有些牽強附會了。
現在在前一篇文章中的創建的控件代碼中添加一個Scope屬性:
[Browsable(true)]
public Scope Scope
{
get
{
return _scope;
}
set
{
_scope = value;
}
}
這個屬性的類型是Scope類,代碼如下:
public class Scope
{
private Int32 _min;
private Int32 _max;
![]()
public Scope()
{
}
![]()
public Scope(Int32 min, Int32 max)
{
_min = min;
_max = max;
}
![]()
[Browsable(true)]
public Int32 Min
{
get
{
return _min;
}
set
{
_min = value;
}
}
![]()
[Browsable(true)]
public Int32 Max
{
get
{
return _max;
}
set
{
_max = value;
}
}
}
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類的類型轉換器為屬性提供了實例化的代碼。
現在在前一篇文章中的創建的控件代碼中添加一個Scope屬性:
[Browsable(true)]
public Scope Scope
{
get
{
return _scope;
}
set
{
_scope = value;
}
}
public class Scope
{
private Int32 _min;
private Int32 _max;
public Scope()
{
}
public Scope(Int32 min, Int32 max)
{
_min = min;
_max = max;
}
[Browsable(true)]
public Int32 Min
{
get
{
return _min;
}
set
{
_min = value;
}
}
[Browsable(true)]
public Int32 Max
{
get
{
return _max;
}
set
{
_max = value;
}
}
}
添加完屬性后,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
我們修改默認的值,然后看看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";


浙公網安備 33010602011771號