从MSDN总结过来的PropertyGrid中ExpandableObjectConverter的应用

来源:互联网 发布:c语言printf和scanf 编辑:程序博客网 时间:2024/05/22 14:00
 public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            AppSettings p = new AppSettings();            this.propertyGrid1.SelectedObject = p;        }    }    /// <summary>    /// 对象转换器    /// </summary>    public class SpellingOptionsConverter : ExpandableObjectConverter    {        /// <summary>        /// 可否转换到目标类型        /// </summary>        /// <param name="context"></param>        /// <param name="destinationType">目标类型</param>        /// <returns></returns>        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)        {            if (destinationType == typeof(SpellingOptions))                return true;            return base.CanConvertTo(context, destinationType);        }        /// <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, CultureInfo culture, object value, System.Type destinationType)        {            if (destinationType == typeof(System.String) &&                 value is SpellingOptions)            {                SpellingOptions so = (SpellingOptions)value;                return "Check while typing:" + so.SpellCheckWhileTyping +                       ", check CAPS: " + so.SpellCheckCAPS +                       ", suggest corrections: " + so.SuggestCorrections;            }            return base.ConvertTo(context, culture, value, destinationType);        }        /// <summary>        /// 可否从源类型转换        /// </summary>        /// <param name="context"></param>        /// <param name="sourceType">源类型</param>        /// <returns></returns>        public override bool CanConvertFrom(ITypeDescriptorContext context, System.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>        /// <returns></returns>        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)        {            if (value is string)            {                try                {                    string s = (string)value;                    int colon = s.IndexOf(':');                    int comma = s.IndexOf(',');                    if (colon != -1 && comma != -1)                    {                        string checkWhileTyping = s.Substring(colon + 1,                                                        (comma - colon - 1));                        colon = s.IndexOf(':', comma + 1);                        comma = s.IndexOf(',', comma + 1);                        string checkCaps = s.Substring(colon + 1,                                                        (comma - colon - 1));                        colon = s.IndexOf(':', comma + 1);                        string suggCorr = s.Substring(colon + 1);                        SpellingOptions so = new SpellingOptions();                        so.SpellCheckWhileTyping = Boolean.Parse(checkWhileTyping);                        so.SpellCheckCAPS = Boolean.Parse(checkCaps);                        so.SuggestCorrections = Boolean.Parse(suggCorr);                        return so;                    }                }                catch                {                    throw new ArgumentException(                        "Can not convert '" + (string)value +                                           "' to type SpellingOptions");                }            }            return base.ConvertFrom(context, culture, value);        }    }

http://msdn.microsoft.com/zh-cn/library/aa302326.aspx

原创粉丝点击