答案:改变 PropertyGrid 控件的编辑风格(4)——加入选择列表
张昱e-zhangyu@vip.sina.com
效果:
适用场合:
限制选择输入
步骤一:定义从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