propertygrid 应用集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanghui_hn/article/details/41804377
property基本用法:http://blog.csdn.net/luyifeiniu/article/details/5426960

隐藏属性:
    在属性前加一个标签[Browsable(false)].
属性分组:
    在属性前加一个分组标签[CategoryAttribute("分组名字")]
子成员展开:
    在子成员对应的class前加上标签: [TypeConverter(typeof(ExpandableObjectConverter))]
     [TypeConverter(typeof(ExpandableObjectConverter))]
     public class subClass
    {
        #成员1
        #成员2
    }

#应用场景1:显示列表#

    参考:http://www.codeproject.com/Articles/4448/Customized-display-of-collection-data-in-a-Propert 
    当一个类中有集合属性时,为了让集合中的元素可以显示,需要实现一个集合MyCollection,实现ICustomDescriptor接口,重载CollectionBase的函数
     class MyCollection : CollectionBase, ICustomTypeDescriptor
     {
        # 实现CollectionBase
        # 实现ICustomTypeDescriptor
     }

#应用场景2:将属性的编辑设置为下拉框ComboBox#    

    参考:http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx
    上面链接中【Type Converters That Provide a List of Standard Values to a
    Properties Window】部分讲解了如何将一个int属性的编辑设置为下拉框


#应用场景3:在属性框中,不展开class,直接将class的某个属性作为class的value#

    参考:http://msdn.microsoft.com/en-us/library/87926x56(v=vs.100).aspx
    这个场景表达起来很费劲,用代码表示
    class classA 
    {
      private classB _attriB;
      private int _index;


      pubic classB AttriB
      {
          get {return this._attriB;}
          set {this._attriB = value;}
      }


      public int Index;
      {
          get {return this._index;}
          set {this._index = value;}
      }
    }
    
    class classB
    {
        private string _value = "123";
        public string Value
        {
            get {return this._value}
            set {this._value = value}
        }


        ###
        //-------
        private string _name;
        public string Name
        {
            get {return this._name;}
            set {this._name = value;}
        } 
        ###
    }

    在main函数中
    classA objA = new classA();
    this.propertygrid.selectobject = objA;
    
    如果我将隐藏classB的其他属性,而将classB.Value作为AttriB的属性值。
    这种情况下,该怎么做?
    借助TypeConverter的四个函数
    CanConverterTo,CanConverterFrom,ConvertTo,ConverterFrom。
    从四个函数名,就可以知道每个函数是干什么的。
   
    public class classB_Converter:TypeConverter
    {
        // 判断classB是否能够从sourceType中转化
         public override bool CanConvertFrom(
        ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }


        // 判断classB是否能够将classB转化为DestinationType
        public override bool CanConvertTo(
            ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }


        // 将value转化为classB
        public override object ConvertFrom(ITypeDescriptorContext
            context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                return new classB();
            }


            if (value is string)
            {
                string s = (string)value;
                if (s.Length == 0)
                {
                    return new classB();
                }
                
                classB b = new classB();
                b.Value = s;
                return b;
            }


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


        // 将classB转化为一个需要的object
        public override object ConvertTo(
            ITypeDescriptorContext context,
            CultureInfo culture, object value, Type destinationType)
        {
            if (value != null)
            {
                if (!(value is classB))
                {
                    throw new ArgumentException(
                        "无效的输入", value.ToString());
                }
            }


            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return String.Empty;
                }


                classB obj = value as classB;
                return obj.Value;
            }


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

上面ConvertFrom返回的是一个new的新对象,如果想保留原对象,只是修改其中得value字段,需要从ConvertFrom函数的第一个参数ITypeDescriptorContext入手,对应的msdn链接地址


#应用场景4:自定义属性的顺序#

参考:Ordering Items in PropertyGrid

从codeproject上找到一个例子,可以自定义属性在属性框的顺序。

猜你喜欢

转载自blog.csdn.net/zhanghui_hn/article/details/41804377