C# PropertyGrid控件的四个自定义操作

1>PropertyGid 控件输入时显示隐藏密码为(*)


单独写一个PasswordStringConverter 类;

using System.ComponentModel;
using System.Globalization;

namespace Test
{
    public class PasswordStringConverter : StringConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.GetType() == typeof(string))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value.GetType() == typeof(string))
            {
                int stringSize;
                string retVal = "* ";
                Random randomString = new Random();


                if (value != null)
                    stringSize = ((string)value).Length;
                else
                    stringSize = randomString.Next(10);

                for (int i = 0; i < stringSize; i++)
                    retVal += "* ";

                return retVal;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return false;
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            string[] standardValues = new string[1];
            int stringSize;
            string retVal = "* ";
            Random randomString = new Random();


            stringSize = randomString.Next(10);

            for (int i = 0; i < stringSize; i++)
                retVal += "* ";

            standardValues[0] = retVal;

            return new StandardValuesCollection(standardValues);
        }
    }
}

调用;

        private string _pwd;

        [CategoryAttribute("数据库连接设置"),
         DisplayNameAttribute("密码"),
         DescriptionAttribute("连接数据库的密码"),
         TypeConverter(typeof(PasswordStringConverter)),  //关键(重载)
        ]
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }

效果:

C# PropertyGrid控件的四个自定义操作_第1张图片


2>PropertyGid 控件选择下拉框值


单独写一个SetTimeItem 类; //@! SetTimeItem 随便建

using System.ComponentModel;

namespace Test
{
    public class SetTimeItem : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            //@! 编辑下拉框中的items
            return new StandardValuesCollection(new string[] { "30", "60" ,"180" }); 
        }
        //@! true: disable text editting.    false: enable text editting;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

调用;

  private string _SetTime;

  [DisplayNameAttribute("时隔时间设置"),
  CategoryAttribute("时间设置"),
  DescriptionAttribute("设置读取服务时隔时间"),
  TypeConverter(typeof(SetTimeItem)),
  ]
  public string SetTime
  {
   get { return _SetTime; }
   set { _SetTime = value; }
  }

效果;

C# PropertyGrid控件的四个自定义操作_第2张图片


3>PropertyGid 控件排序

(如果不排序,它会自动按照拼音首字母给你排序,排版变得比较丑)


单独写一个PropertySorter类;

using System;
using System.Collections;
using System.ComponentModel;

namespace Test
{
    public class PropertySorter : ExpandableObjectConverter
    {

        #region Methods
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            //
            // Thise 这个overrid返回属性的列表
            //
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
            ArrayList orderedProperties = new ArrayList();
            foreach (PropertyDescriptor pd in pdc)
            {
                Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
                if (attribute != null)
                {
                    //
                    // 如果找到的属性,然后创建一个对象持有它
                    //
                    PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
                    orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
                }
                else
                {
                    //
                    // 没有这个属性就给它一个0
                    //
                    orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
                }
            }
            //
            // 执行实际的使用价值PropertyOrderPair类IComparable排序的实现
            //
            orderedProperties.Sort();
            //
            // 构建一个字符串列表的命令名称
            //
            ArrayList propertyNames = new ArrayList();
            foreach (PropertyOrderPair pop in orderedProperties)
            {
                propertyNames.Add(pop.Name);
            }
            //
            // 通过PropertyDescriptorCollection排序的有序列表
            //
            return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
        }
        #endregion
    }

    #region Helper Class - PropertyOrderAttribute
    [AttributeUsage(AttributeTargets.Property)]
    public class PropertyOrderAttribute : Attribute
    {
        //
        //简单属性允许指定一个属性的顺序
        //
        private int _order;
        public PropertyOrderAttribute(int order)
        {
            _order = order;
        }

        public int Order
        {
            get
            {
                return _order;
            }
        }
    }
    #endregion

    #region Helper Class - PropertyOrderPair
    public class PropertyOrderPair : IComparable
    {
        private int _order;
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
        }

        public PropertyOrderPair(string name, int order)
        {
            _order = order;
            _name = name;
        }

        public int CompareTo(object obj)
        {
            //
            // 结对对象通过排序顺序的值
            // 平等的价值观得到相同的等级
            //
            int otherOrder = ((PropertyOrderPair)obj)._order;
            if (otherOrder == _order)
            {
                //
                // 如果未指定顺序,按名称排序
                //
                string otherName = ((PropertyOrderPair)obj)._name;
                return string.Compare(_name, otherName);
            }
            else if (otherOrder > _order)
            {
                return -1;
            }
            return 1;
        }
    }
    #endregion
}


//@! 瞎扯淡,我也不没仔细看代码,不过我要的功能达到,可以看看效果图

调用;
(在PropertyGrid属性类里面添加)

using System.ComponentModel;
using Utility.SW.Common;
using Utility.SW.NRegistry;

namespace Test
{
    [TypeConverter(typeof(PropertySorter))] //@! 用来排序
    public class TestConfig   
    {         
        private string _ip;
        private string _database;
        private string _uid;
        private string _pwd;

        #region 数据库设置
        [CategoryAttribute("数据库连接设置"), 
         DisplayNameAttribute("服务器名称"),       
         DescriptionAttribute("数据库服务器名(IP)"),
         PropertyOrderAttribute(1)  //@! 序号(排序)
        ]
        public string IP
        {
            get { return _ip; }
            set { _ip = value; }
        }

        [CategoryAttribute("数据库连接设置"),
         DisplayNameAttribute("数据库名称"),       
         DescriptionAttribute("连接的数据库名"),
         PropertyOrderAttribute(2)
        ]
        public string Database
        {
            get { return _database; }
            set { _database = value; }
        }

        [CategoryAttribute("数据库连接设置"),
         DisplayNameAttribute("用户名"),
         DescriptionAttribute("连接数据库的用户名"),
         PropertyOrderAttribute(3)
        ]
        public string UId
        {
            get { return _uid; }
            set { _uid = value; }
        }

        [CategoryAttribute("数据库连接设置"),
         DisplayNameAttribute("密码"),
         DescriptionAttribute("连接数据库的密码"),
         PropertyOrderAttribute(4)
        ]
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }
        #endregion
    }
}

效果;

C# PropertyGrid控件的四个自定义操作_第3张图片

不排序的效果是
C# PropertyGrid控件的四个自定义操作_第4张图片


4>PropertyGrid属性排序

下面的连接是他们写的点击直接转到他这个只是在当前类上面添加

http://greatverve.cnblogs.com/archive/2012/02/20/propergrid-category-order.html

还有种是加(/t)

class Test
{
    [Category("Advanced")]
    public int Age { get; set; }

     //@! 
    [Category("\tGeneral")]       //<--
    public string Name { get; set; }
}

嘻嘻,这些东西都是网上有的资源,我也是借鉴..写在一起了,以后就容易可以找到些,

你可能感兴趣的:(C#)