您的位置:首页 > 编程语言 > ASP

Asp.Net 控件开发之属性转换器

2009-09-29 10:47 357 查看
不知道大家注意到没有,上次在写复杂属性时,定义了一个Company的类,但是显示属性时,虽然子属性出来了!主属性那里却是:GoldWisdom.Company这个看着很让人讨厌.可是怎么把它干掉呢?

下面我们来搞定它:

一、效果图


改之前:



应用转换器之后



哈哈,还是改完之后看着比较爽

二、 代码实现

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Globalization;
namespace GoldWisdom
{
    /// <summary>
    /// 指定Company的类型转换器
    /// </summary>
    [TypeConverter(typeof(CompanyTypeConverter))]
    public class CompanyTypeConverter:ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;
            return base.CanConvertTo(context, destinationType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
                return new Company();
            }
            if(value is string)
            {
                string s = (string)value;
                if (s.Length == 0)
                    return new Company();
                string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
                if (parts.Length != 3)
                {
                    throw new ArgumentException("不可识别的公司信息", "value");
                }
                //返回指定类型转换器
                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
                return new Company((string)stringConverter.ConvertFromString(context, culture, parts[0]),
                    (string)stringConverter.ConvertFromString(context, culture, parts[1]),
                    (string)stringConverter.ConvertFromString(context, culture, parts[2]),
                    Convert.ToInt32(parts[3]));
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
            {
                if (!(value is Company))
                {
                    throw new ArgumentException("不可识别的公司信息", "value");
                }
            }
            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return "";
                }
                Company cp = (Company)value;
                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
                return String.Join(culture.TextInfo.ListSeparator,
                    new string[] {
                                     stringConverter.ConvertToString(context, culture, cp.Name),
                                     stringConverter.ConvertToString(context, culture, cp.Address),
                                     stringConverter.ConvertToString(context, culture, cp.Procudure),
                                     stringConverter.ConvertToString(context, culture, cp.EmployeeCount.ToString())
                                 });
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override string ToString()
        {
            return ToString(CultureInfo.CurrentCulture);
        }
        public virtual string ToString(CultureInfo culture)
        {
            return TypeDescriptor.GetConverter(typeof(Company)).ConvertToString(null, culture, this);
        }
    }
}



写完之后要在控件的属性前加入自定义的转换器

private  Company _company =new Company();
        //_company一定要实例化,否则属性窗口只显示一个输入框
        [Browsable(true),
        Category("扩展属性"),
        DefaultValue("Red"),
        Description("折叠属性描述")]
        //[TypeConverter(typeof(ExpandableObjectConverter))] //默认转换器
        [TypeConverter(typeof(CompanyTypeConverter))]
        //指定属性设计器当前属性为折叠形式的
        public Company CompanyInfo
        {
            get { return _company; }
        }



三、原理

没有应用之前,属性浏览器无法识别自定义的Company类是什么东东。不知道怎么显示。给他自定义一个以后,他就知道了!不过有一个小疑问没有搞定,当我什么属性也没有填时,主属性那里显示的是以逗号分格的一个串。而不是像FONT属性那样空白。看来还得好好学习一下。有明白有大鸟请指点一下!



至于其它方面的转换器看情况,跟贴或新开贴再说
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: