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

解决方案 »

  1.   


        [TypeConverter(typeof(ExpandableObjectConverter))]
        public class ProjectTableItem:IDisposable
      

  2.   

    1楼说的是转换器,我是想实现不同级别的属性应该有一个层次,就是左边有一个缩进,如下图,就是让"TextFont”前面的"+"和TextColor对齐。
      

  3.   

    这里有个很好的示例,供参考http://blog.csdn.net/luyifeiniu/article/details/5426960
      

  4.   

    以前写过一个类似的功能 不知道是不是楼主想要的 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);
            }  
        }
      

  5.   

    6楼说的在理,我按照这个思路,做了测试,代码如下:
    //上级属性
    [CategoryAttribute("显示设置"), DescriptionAttribute("文本字体设置"), EditorAttribute(typeof(MyFont), typeof(UITypeEditor))]
    public MyFont TextFont
    {
         get { return fHeaderFont; }
         set { fHeaderFont = value; }
    }
    //下级属性
    public class MyFont : UITypeEditor
        {
            private Font headerFont = new Font("宋体", 9, FontStyle.Regular);
            private int index = 0;        [CategoryAttribute("显示设置"), DescriptionAttribute("文本字体设置")]
            public Font HeaderFont
            {
                get { return headerFont; }
                set { headerFont = value; }
            }        [CategoryAttribute("显示设置"), DescriptionAttribute("测试")]
            public int Index
            {
                get { return index; }
                set { index = value; }
            }        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc != null)
                {
                    if (value != null)
                    {
                        FontDialog dialog = new FontDialog();
                        dialog.ShowDialog();
                    }
                }
                return value;
            }
        }
    现在的FONT属性确实自动缩进了,但是现在又有如下问题:
    (1)需要自定义EditorAttribute,自定义不太直观,涉及到UI跳转;
    (2)如何让子属性类里的属性都显示出来,而且带“+”那样展开,就是想实现多级分组(自定义EditorAttribute貌似只能是下拉列表或者是对话框);