,所以要求转换为字符串格式;反之,会 把字符串逆向转换为 SolidCoordinate 类。这就是类型转换器的功能。
格式SolidCoordinate="3,5,8" 是可以自定义的,比如可以定义成SolidCoordinate="3-5-8" 格式,规则可以在转换器类中任意指定,只要是字符串格式且保证正反向转换规则一致即可。
接下来开始讲解代码部分。SolidCoordinate 类共有三个属性(X ,Y ,Z ),前两个值(X ,Y )与Point 类型的(X ,Y )属性一致,表示平面上的横坐标和纵坐标;(Z )属性表示平面 之外的第三维坐标,类代码如下:
/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
[TypeConverter(typeof(SolidCoordinateConverter))]
public class SolidCoordinate
{
private int x;
private int y;
private int z;
public SolidCoordinate()
{
}
public SolidCoordinate(int x,int y,int z)
{
this.x = x;
this.y = y;
this.z = z;
}
[NotifyParentProperty(true)]
public int x
{
get
{
return this.x;
}
set
{
this.x = value;
}
}
[NotifyParentProperty(true)]
public int Y
{
get
{
return this.y;
}
set
{
this.y = value;
}
}
[NotifyParentProperty(true)]
public int Z
{
get
{
return this.z;
}
set
{
this.z = value;
}
}
}
窃旗鷹祥淫凄眉倖恫炎奉來?短嗤販採圭隈。俶勣廣吭議頁貧中嗤鞘?
[TypeConverter(typeof(SolidCoordinateConverter))]
凪恬喘頁峺協乎窃議廬算匂葎SolidCoordinateConverter ?軸群頁SolidCoordinate 窃侏議陣 周奉來脅氏聞喘緩窃侏廬算匂。
SolidCoordinateConverter 窃議坿旗鷹泌和?
/// <summary>
/// 資誼云慕厚謹坪否,萩心:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
public class SolidCoordinateConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType)
{
return ((sourceType == typeof(string)) || base.CanConvertFrom (context,sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType)
{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context,destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value)
{
string str = value as string;
if (str == null)
{
return base.ConvertFrom(context,culture,value);
}
string str2 = str.Trim();
if (str2.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char ch
|