首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# PropertyGrid解决方案

2013-10-24 
C# PropertyGrid各位大侠,最近在用PropertyGrid做项目,遇到一个问题:PropertyGrid控件进行分组后,复合类型

C# PropertyGrid
各位大侠,最近在用PropertyGrid做项目,遇到一个问题:PropertyGrid控件进行分组后,复合类型的属性,例如“字体”类型的属性,在归类后,如何让它左边有缩进呢?像下图那样?C# PropertyGrid解决方案


 ColorProperty m_cp;
        public ColorProperty 颜色
        {
            get
            {
                if (m_cp == null)
                    m_cp = new ColorProperty("0x000000");

                return m_cp;
            }
            set { m_cp = value; }
        }



 [TypeConverter(typeof(ColorPropertyConverter))]
    public class ColorProperty
    {
        string colorstr = "0x000000";
        public string 颜色值
        {
            get 
            {
                if (colorstr.Length != 8)
                    colorstr = "0x000000"; 
                return colorstr; 
            }
            set 
            { 
                if(value.Length==8)
                    colorstr = value; 
            }
        }

        public Color 颜色
        {
            get { return UInt32ToColor(colorstr); }
            set { colorstr = ColorToUInt32(value);}
        }

        public ColorProperty(string str)
        {
            colorstr = str;
        }

        public ColorProperty(Color color)
        {
            颜色 = color;
        }

        public string ReturnColorStr()
        {
            return colorstr;
        }

        public string ColorToUInt32(Color color)
        {
            string r = Convert.ToString(color.R, 16);
            if (r.Length == 1) r = "0" + r;
            string g = Convert.ToString(color.G, 16);
            if (g.Length == 1) g = "0" + g;


            string b = Convert.ToString(color.B, 16);
            if (b.Length == 1) b = "0" + b;
            return "0x" + (r + g + b).ToLower();
        }

        public Color UInt32ToColor(string aa)
        {
                string A = "ff";
                string R = aa.Substring(2, 2);
                string G = aa.Substring(4, 2);
                string B = aa.Substring(6, 2);

                return Color.FromArgb(Convert.ToInt32(A, 16), Convert.ToInt32(R, 16), Convert.ToInt32(G, 16), Convert.ToInt32(B, 16));

        }


    }

    public class ColorPropertyConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
        {
            if (destinationType == typeof(ColorProperty))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
        {
            if (destinationType == typeof(System.String) && value is ColorProperty)
            {
                ColorProperty so = (ColorProperty)value;

                return so.颜色值;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, System.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)
            {
                try
                {
                    ColorProperty so = new ColorProperty((string)value);

                    return so;
                }
                catch
                {
                    throw new ArgumentException("格式错误");
                }
            }
            return base.ConvertFrom(context, culture, value);
        }  


    }


[解决办法]
只要这个属性是一个类,那么它的下级属性就会自动缩进的。

热点排行