弗瑞斯达

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

运行效果:

解决方案:

 

MainForm.cs
 public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
CProvincialCapital proCap = new CProvincialCapital("南京市", 10000000);
CProvince pro = new CProvince("江苏省", proCap, 8);
CCountry cou = new CCountry("中国", pro);

propertyGrid1.SelectedObject = cou;
}
}
CCountry.cs
public class CCountry
{
public CCountry(string name, CProvince pro)
{
_Province = pro;
_Name = name;
}

private string _Name;
[DisplayName("名称")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}

private CProvince _Province;
[DisplayName("省份")]
public CProvince Province
{
get { return _Province; }
set { _Province = value; }
}
}
CProvince.cs
    [TypeConverter(typeof(ConverterPro))]
public class CProvince
{
public CProvince(string name, CProvincialCapital provincialCapital, int district)
{
_Name = name;
_ProvincialCapital = provincialCapital;
_District = district;
}

private string _Name;
[DisplayName("名称")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}

private CProvincialCapital _ProvincialCapital;
[DisplayName("省会")]
public CProvincialCapital ProvincialCapital
{
get { return _ProvincialCapital; }
set { _ProvincialCapital = value; }
}

private int _District;
[DisplayName("地区数量")]
public int District
{
get { return _District; }
set { _District = value; }
}
}
CProvincialCapital.cs
    [TypeConverter(typeof(ConverterProCap))]
public class CProvincialCapital
{
public CProvincialCapital(string name, int population)
{
_Name = name;
_Population = population;
}

private string _Name;
[DisplayName("名称")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}

private int _Population;
[DisplayName("人口数量")]
public int Population
{
get { return _Population; }
set { _Population = value; }
}
}
ConverterPro.cs
public class ConverterPro : ExpandableObjectConverter
{
/// <summary>
/// 返回该转换器是否可以使用指定的上下文将该对象转换为指定的类型
/// </summary>
/// <param name="context"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(CProvince))
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

/// <summary>
/// 返回该转换器是否可以使用指定的上下文将给定类型的对象转换为此转换器的类型
/// </summary>
/// <param name="context">提供有关组件的上下文,如其容器和属性描述符</param>
/// <param name="sourceType"></param>
/// <returns></returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}

return base.CanConvertFrom(context, sourceType);
}

/// <summary>
/// 使用指定的上下文和区域性信息将给定的值对象转换为指定的类型
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if (destinationType == typeof(string) &&
value is CProvince)
{
CProvince pro = (CProvince)value;

return pro.Name + "," + pro.District + "," + pro.ProvincialCapital.Name +
"," + pro.ProvincialCapital.Population.ToString();
}

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


public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
string s = (string)value;
string[] ss = s.Split(',');

CProvincialCapital proCap = new CProvincialCapital(ss[1].ToString(), int.Parse(ss[2]));
CProvince pro = new CProvince(ss[0].ToString(), proCap, int.Parse(ss[3]));
return pro;
}

return base.ConvertFrom(context, culture, value);
}
}
ConverterProCap.cs
    public class ConverterProCap : ExpandableObjectConverter
{
/// <summary>
/// 返回该转换器是否可以使用指定的上下文将该对象转换为指定的类型
/// </summary>
/// <param name="context"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(CProvincialCapital))
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

/// <summary>
/// 返回该转换器是否可以使用指定的上下文将给定类型的对象转换为此转换器的类型
/// </summary>
/// <param name="context">提供有关组件的上下文,如其容器和属性描述符</param>
/// <param name="sourceType"></param>
/// <returns></returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}

return base.CanConvertFrom(context, sourceType);
}

/// <summary>
/// 使用指定的上下文和区域性信息将给定的值对象转换为指定的类型
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if (destinationType == typeof(string) &&
value is CProvincialCapital)
{
CProvincialCapital cou = (CProvincialCapital)value;
string name = cou.Name;
int population = cou.Population;

return name.ToString() + "," + population.ToString();
}

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


public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
string s = (string)value;
string[] ss = s.Split(',');

CProvincialCapital cou = new CProvincialCapital(ss[0].ToString(), int.Parse(ss[1]));
return cou;
}

return base.ConvertFrom(context, culture, value);
}
}
posted on 2012-03-24 15:20  弗瑞斯达  阅读(2066)  评论(1编辑  收藏  举报