自定义组件之属性(Property)的性质(Attribute)介绍(三)

2 、展开的形式

展开的形式多用于一个属性为我们自定义类的类型,比如我们定义了一个类,该类中的一个属性是另一个我们定义的类。在这种情况下属性浏览器默认是没有办法来进行类型转换的,所以显示为不可编辑的内容。如果我们要以展开的形式编辑这个属性就需要我们向上面一样来重写属性转换器。

我们首先定义一个自己的类来作为以后的属性类型。具体代码如下:

public class ExpandProperty

{

private int _intList=0;

public int IntList

{

get { return this._intList;}

set { this._intList=value; }

}

private string _strList="Null";

public string StrList

{

get { return this._strList;}

set { this._strList= value;}

}

}

然后我们在自己的另一个类中声明一个这个类型的属性,在这里如果我们不加任何的性质限制,属性浏览器是不能转换改属性的。具体实现该属性的代码如下:

private ExpandProperty _dropList;

[CategoryAttribute(" 自定义的复杂类型设置 ( 包括自定义类型转换器 )"),

TypeConverterAttribute(typeof(PropertyGridApp.ExpandConverter)),

ReadOnlyAttribute(false)]

public ExpandProperty DropList

{

get { return this._dropList;}

set { this._dropList= value;}

}

为了让属性浏览器能够编辑该属性,也就是说能够把该属性转换成字符串,而且能够从字符串转换成该类的一个实例需要我们写如下的代码:

///

1<summary>
2
3///  可以展开的类型转换器 
4
5///  ExpandProperty 
6
7/// </summary>

public class ExpandConverter:System.ComponentModel.ExpandableObjectConverter

{

public ExpandConverter()

{

}

///

1<summary>
2
3///  覆盖此方法已确定属性是否可以转换 
4
5/// </summary>

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)

{

if (destinationType==typeof(PropertyGridApp.ExpandProperty))

return true;

return base.CanConvertTo(context,destinationType);

}

///

1<summary>
2
3///  覆盖此方法并确保  destinationType  参数是一个  String  ,然后格式化所显示的内容 
4
5/// </summary>

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

{

if (destinationType == typeof (System.String) && value is PropertyGridApp.ExpandProperty)

{

PropertyGridApp.ExpandProperty source=(PropertyGridApp.ExpandProperty)value;

return source.IntList+","+source.StrList;

}

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

}

///

1<summary>
2
3///  覆盖此方法已确定输入的字符串是可以被转化 
4
5/// </summary>

public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)

{

if (sourceType==typeof(string))

return true;

return base.CanConvertFrom(context,sourceType);

}

///

1<summary>
2
3///  覆盖此方法根据  ConvertTo()  方法的转换格式来把所输入的字符串转换成类,并返回该类 
4
5/// </summary>

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

{

if (value is string)

{

string s=(string)value;

int comma=s.IndexOf(",");

if (comma!=-1)

{

try

{

string intList=s.Substring(0,comma);

string strList=s.Substring(comma+1,s.Length-comma-1);

PropertyGridApp.ExpandProperty Ep=new ExpandProperty();

Ep.IntList=int.Parse(intList);

Ep.StrList=strList;

return Ep;

}

catch

{

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

}

}

}

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

}

}

编译之后的画面如下:

Published At
Categories with Web编程
Tagged with
comments powered by Disqus