从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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值