改变 propertygrid 控件的编辑风格(4)——加入选择列表

适用场合:

限制选择输入

 

 

步骤一:定义从uitypeeditor 继承的抽象类:comboboxitemtypeconvert。示例如下:

using system;

using system.collections;

using system.componentmodel;

 

 

namespace blog.csdn.net.zhangyuk

{

     /// imstypeconvert 的摘要说明。

     /// </summary>

     public abstract class comboboxitemtypeconvert : typeconverter

     {

         public hashtable _hash = null;

 

 

         public comboboxitemtypeconvert()

         {

              _hash = new hashtable();

              getconverthash();

         }

 

 

         public abstract void getconverthash();

 

 

         public override bool getstandardvaluessupported( itypedescriptorcontext context )

         {

              return true;

         }

 

 

         public override standardvaluescollection getstandardvalues(

itypedescriptorcontext context)

         {

              int [] ids = new int [ _hash.values.count ];

 

 

              int i=0;

              foreach (dictionaryentry myde in _hash)

              {

                   ids[i++] = (int)(myde.key);

              }

 

 

              return new standardvaluescollection( ids );

         }

 

         public override bool canconvertfrom( itypedescriptorcontext context, type sourcetype)

         {

              if (sourcetype == typeof(string))

              {

                   return true;

              }

              return base.canconvertfrom(context, sourcetype);

         }

 

 

         public override object convertfrom(

itypedescriptorcontext context, 

system.globalization.cultureinfo culture,

object v )

         {

              if (v is string)

              {

                   foreach (dictionaryentry myde in _hash)

                   {

                       if( myde.value.equals((v.tostring())) )

                            return myde.key;

                   }

              }

              return base.convertfrom(context, culture, v);

         }

 

 

         public override object convertto( 

itypedescriptorcontext context, 

system.globalization.cultureinfo culture, object v ,

type destinationtype)

         { 

              if (destinationtype == typeof(string))

              {

                   foreach (dictionaryentry myde in _hash)

                   {

                       if( myde.key.equals(v) )

                            return myde.value.tostring();

                   }

                   return "";

              }

              return base.convertto(context, culture, v, destinationtype);

         }

 

 

         public override bool getstandardvaluesexclusive(

              itypedescriptorcontext context)

         {

              return false;

         }       

     }

}

 

 

步骤二:定义 comboboxitemtypeconvert 的派生类,派生类中实现父类的抽象方法:

public abstract void getconverthash();

示例如下:

 

 

using system;

using system.collections;

using system.componentmodel;

 

 

namespace blog.csdn.net.zhangyuk

{

     public class propertygridboolitem : comboboxitemtypeconvert

     {

         public override void getconverthash()

         {

              _hash.add(0,"是");

              _hash.add(1,"否");

         }

     }

 

 

     public class propertygridcomboboxitem : comboboxitemtypeconvert

     {

         public override void getconverthash()

         {

              _hash.add(0,"炒肝");

              _hash.add(1,"豆汁");

              _hash.add(2,"灌肠");

         }

     }

}

 

 

步骤三:编辑属性类,指定编辑属性。示例如下:

namespace blog.csdn.net.zhangyuk

{

         public class someproperties

     {

         private string _finished_time   = "";

                   ……

         // 布尔

         bool _bool = true;

         [

              description("布尔"),

              category("属性"),

              typeconverter(typeof( propertygridboolitem ))

         ]

         public int 布尔

         {

              get { return _bool == true ? 0 : 1; }

              set { _bool = (value == 0 ? true : false); }

         }

 

 

         // 选择列表

         int _comboboxitems = 0;

         [

              description("选择列表"),

              category("属性"),

              typeconverter(typeof( propertygridcomboboxitem ))

         ]

         public int 选择列表

         {

              get { return _comboboxitems;  }

              set { _comboboxitems = value; }

         }

         ……

         }

}

 

 

步骤四:设置propertygrid的属性对象。示例如下:

         private void form1_load(object sender, system.eventargs e)

         {

            this.propertygrid1.selectedobject = new someproperties();

         } 

你可能感兴趣的:(改变 propertygrid 控件的编辑风格(4)——加入选择列表)