WinForm 自定义类型属性的设计器支持

引言

通常winform系统类型变量,如 double、string、float等类型属性会被属性窗口自动识别添加,使得我们可以手动输入其值。但如果是自定义类型属性时,属性窗口却会灰掉,无法修改。这时需要自动应以类型转换TypeConverter。

    public class MyProperty
    {
        public string Name { get; set; } = "property";
        public int Value { get; set; }
    }

    public class MyClass
    {
        public MyProperty property { get; set; } = new MyProperty();

    }

 1、TypeConverter类型转换

实现自定义类型的转换方法 继承自TypeConverter

    [TypeConverter(typeof(MyPropertyConverter))]
    public class MyProperty
    {
        public string Name { get; set; } = "property";
        public int Value { get; set; }
    }
    public class MyPropertyConverter:TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] v = ((string) value).Split(',');
                if (v.Length != 2)
                {
                    throw new ArgumentException("input must be of form ");
                }
                MyProperty mp = new MyProperty();
                mp.Name = v[0];
                mp.Value = int.Parse(v[1]);
                return mp;
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                MyProperty mp = (MyProperty) value;
                return mp.Name + "," + mp.Value;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                MyProperty mp = (MyProperty) value;
                return new InstanceDescriptor(typeof(MyProperty).GetConstructor(new Type[] { }),null);
                ;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

2、展开形式

参数如果过多,类型复杂,上述方法修改起来不太方便,可以改用展开类型

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class MyProperty
    {
        public string Name { get; set; } = "property";
        public int Value { get; set; }
    }

 3、上述两种的组合形式

    [TypeConverter(typeof(MyPropertyConverter))]
    public class MyProperty
    {
        public string Name { get; set; } = "property";
        public int Value { get; set; }
    }
    public class MyPropertyConverter: ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] v = ((string) value).Split(',');
                if (v.Length != 2)
                {
                    throw new ArgumentException("input must be of form ");
                }
                MyProperty mp = new MyProperty();
                mp.Name = v[0];
                mp.Value = int.Parse(v[1]);
                return mp;
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                MyProperty mp = (MyProperty) value;
                return mp.Name + "," + mp.Value;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                MyProperty mp = (MyProperty) value;
                return new InstanceDescriptor(typeof(MyProperty).GetConstructor(new Type[] { }),null);
                ;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

你可能感兴趣的:(c#,Winform,Winform,c#)