【轉】PropertyGrid控制項中的多級顯示

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   os   io   for   

運行效果:

解決方案:

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.cspublic 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.cspublic 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.