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

当WinForm中的属性是自定义类型时,属性窗口无法直接编辑。通过实现TypeConverter,可以允许从字符串转换到自定义类型,使属性窗口可编辑。此外,使用ExpandableObjectConverter可以让复杂类型展开,便于在属性窗口中配置。组合两者可以提供更灵活的编辑体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

通常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 <Name,Value>");
                }
                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 <Name,Value>");
                }
                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);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James.TCG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值