從MSDN總結過來的PropertyGrid中ExpandableObjectConverter的應用

 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章