zoukankan      html  css  js  c++  java
  • PropertyGrid控件中的多级显示

    运行效果:

    解决方案:

    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);
    }
    }
  • 相关阅读:
    学习进度博客六
    Ultimate四则运算
    水骑士团队介绍
    返回一个二维整数数组中最大联通子数组的和
    学习进度博客五
    构建之法阅读笔记02
    四则运算4
    敏捷开发方法综述
    第一冲刺阶段站立会议02
    学习进度表_七周
  • 原文地址:https://www.cnblogs.com/2008freestyle/p/2415528.html
Copyright © 2011-2022 走看看