WINFORM.PropertyGrid学习(二)

WINFORM.PropertyGrid学习(一)中只列出了简单的效果,且控件中只有简单的属性。如果控件中需要显示像Size、Font、Color等复杂的自定义属性该如何实现呢?此时需要一个转换器,将自定义的类型,转换成PropertyGrid可以显示的类型。

///自定义转换器类,派生于TypeConverter
public class MyPointConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string text = value as string;
            if (text == null)
            {
                return base.ConvertFrom(context, culture, value);
            }
            string text2 = text.Trim();
            if (text2.Length == 0)
            {
                return null;
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            char separator = culture.TextInfo.ListSeparator[0];
            string[] array = text2.Split(new char[] { separator }, StringSplitOptions.None);
            int[] array2 = new int[array.Length];
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
            for (int i = 0; i < array2.Length; i++)
            {
                array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
            }
            if (array2.Length == 2)
            {
                return new MyPonit(array2[0], array2[1]);
            }
            throw new ArgumentException("");
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (value is MyPonit)
            {
                MyPonit point = (MyPonit)value;
                if (destinationType == typeof(string))
                {
                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }
                    string separator = culture.TextInfo.ListSeparator + " ";
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                    string[] value2 = new string[]
                    {
                converter.ConvertToString(context, culture, point.XX),
                converter.ConvertToString(context, culture, point.Y)
                    };
                    return string.Join(separator, value2);
                }
                if (destinationType == typeof(InstanceDescriptor))
                {
                    ConstructorInfo constructor = typeof(MyPonit).GetConstructor(new Type[]
                    {
                typeof(int),
                typeof(int)
                    });
                    if (constructor != null)
                    {
                        return new InstanceDescriptor(constructor, new object[]
                        {
                    point.XX,
                    point.Y
                        });
                    }
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            if (propertyValues == null)
            {
                throw new ArgumentNullException("propertyValues");
            }
            object obj = propertyValues["XX"];
            object obj2 = propertyValues["Y"];
            if (obj == null || obj2 == null || !(obj is int) || !(obj2 is int))
            {
                throw new ArgumentException("");
            }
            return new MyPonit((int)obj, (int)obj2);
        }
        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(MyPonit), attributes);
            return properties.Sort(MyPointConverter.s_propertySort);

        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        private static readonly string[] s_propertySort = new string[] { "XX", "Y" };

    }

[TypeConverter(typeof(MyPointConverter))]//转换器用于复杂类型
public class MyPonit//自定义的复杂类型,需要在空间中显示
    {
        int x;
        int y;
        public int XX
        {
            get { return x; }
            set { x = value; }
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public MyPonit(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

 public class AppSetting
    {
        MyPonit myPonit;
        public MyPonit MyPonit
        {
            get
            {
                if (myPonit == null)
                {
                    return new MyPonit(0, 0);
                }
                else
                {
                    return myPonit;
                }
            }
            set
            {
                myPonit = value;
            }
        }
  }


         //绑定控件
        AppSetting appSetting = new AppSetting();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = appSetting;
           
        }

效果如下:
在这里插入图片描述
参考文章一
参考文章二

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值