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

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

**

张昱 [email protected]

** 效果: **


** **


**

** 适用场合: ** **

**

限制选择输入

** 步骤一 ** :定义从 UITypeEditor 继承的抽象类: ComboBoxItemTypeConvert 。示例如下:

using System;

using System.Collections;

using System.ComponentModel;

namespace blog.csdn.net.zhangyuk

{

/// IMSTypeConvert 的摘要说明。

/// 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;

<span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: &

Published At
Categories with Web编程
Tagged with
comments powered by Disqus