博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

控件属性中使用继承TypeConverter对Color[]与string进行转换

Posted on 2007-05-18 17:07  faib  阅读(1527)  评论(1编辑  收藏  举报
如果控件的属性是一个数组的时候,如何才能将对象保存下来,又如何将它解析出来呢?使用TypeConverter类可以实现这样的转换。比如今天我所遇到的就是Color[]转换到string保存,并从string解析出来。

控件的CustomColor属性如下定义:
        private Color[] m_CustomColor = new Color[0];

        [Category(
"Appearance"),
        TypeConverter(
typeof(ColorConverter)),
        Description(
"自定义字符随机颜色。")]
        
public Color[] CustomColor
        
{
            
get{return m_CustomColor;}
            
set{m_CustomColor = value;}
        }

然后定义ColorConverter类:
using System;
using System.Drawing;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;

namespace FaibClass.WebControls
{
    
internal class ColorConverter : TypeConverter
    
{
        
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;
            }

            
else if (destinationType == typeof(InstanceDescriptor))
            
{
                
return true;
            }


            
return base.CanConvertTo(context, destinationType);
        }


        
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        
{
            
if (value is string)
            
{
                
//从字符串中解析出十六进制格式的颜色值,并转成Color
                string[] value1 = value.ToString().Split(';');
                Color[] col 
= new Color[value1.Length];
                
for(int i = 0; i < value1.Length; i++)
                
{
                    col[i] 
= Util.FromHexColor(value1[i]);
                }

                
return col;
            }


            
return base.ConvertFrom(context, culture, value);
        }


        
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        
{
            
if (destinationType == typeof(string))
            
{
                
//将Color数组转换成 #ffffff;#ccddaa;#00ddcc 这样格式的字符串保存
                Color[] col = (Color[])value;
                
string[] sto = new string[col.Length];
                
for(int i = 0; i < col.Length; i++)
                
{
                    sto[i] 
= Util.ConvertToHexColor(col[i]);
                }

                
return string.Join(";", sto);
            }

            
else if (destinationType == typeof(InstanceDescriptor) && (value is Color[]))
            
{
                Color[] col 
= (Color[])value;
                
string[] sto = new string[col.Length];
                
for(int i = 0; i < col.Length; i++)
                
{
                    sto[i] 
= Util.ConvertToHexColor(col[i]);
                }

                
return new InstanceDescriptor(typeof(Color[]).GetConstructor(new Type[] typeof(string) }), new string[] string.Join(";", sto) } );
            }


            
return base.ConvertTo(context, culture, value, destinationType);
        }

    }

}

以上来用到了两个方法,分别是将颜色转成十六进制格式的值,另一个是反转:
using System.Drawing;
using System.Web.UI.WebControls;

internal class Util
{
        
//将颜色转换为十六进制
        internal static string ConvertToHexColor(Color c)
        
{
            WebColorConverter wcc 
= new WebColorConverter();
            
return wcc.ConvertToString(c);
        }


        
//将颜色转换为十六进制
        internal static Color FromHexColor(string c)
        
{
            WebColorConverter wcc 
= new WebColorConverter();
            
return (Color)wcc.ConvertFromString(c);
        }


}