为WinForm控件的复杂属性提供类型转换器

转帖|其它|编辑:郝浩|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@evget.com

文章转载自:博客转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP