• 【转】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);
            }
        }
    

      

    原文地址:http://www.cnblogs.com/2008freestyle/archive/2012/03/24/2415528.html

  • 相关阅读:
    NOI2010 能量采集
    NOI2011 兔兔与蛋蛋游戏
    动态规划——min/max的单调性优化总结
    NOI2011 NOI嘉年华
    NOI2011 阿狸的打字机
    NOI2011 智能车比赛
    NOI2011 兔农
    NOI2012 魔幻棋盘
    NOI2012 美食节
    NOI2012 迷失游乐园
  • 原文地址:https://www.cnblogs.com/KevinJasmine/p/3911137.html
Copyright © 2020-2023  润新知