我是靠谱客的博主 自由小馒头,最近开发中收集的这篇文章主要介绍从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

最后

以上就是自由小馒头为你收集整理的从MSDN总结过来的PropertyGrid中ExpandableObjectConverter的应用的全部内容,希望文章能够帮你解决从MSDN总结过来的PropertyGrid中ExpandableObjectConverter的应用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部