轉帖|其它|編輯:郝浩|2010-12-06 15:05:44.000|閱讀 527 次
概述:上一篇文章我已經介紹了TypeConverterAttribute元數據的作用,本文將通過代碼向你展示具體的實現。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
上一篇文章我已經介紹了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;
}
}
}
添加完屬性后,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類的類型轉換器為屬性提供了實例化的代碼。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:博客轉載