編寫和顯示訂制的類型
屬性窗口中的編輯有如下三種工作方式
圖
實現下拉的工作同樣是由TypeConverter來定義
舉個例來說
public class FamilyMember : Component
{
private string relation =
[TypeConverter(typeof(RelationConverter))
public string Relation
{
get { return relation;}
set { this
}
}
internal class RelationConverter : StringConverter
{
private static StandardValuesCollection defaultRelations =
new StandardValuesCollection(
new string[]{
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
// returning false here means the property will
// have a drop down and a value that can be manually
// entered
return false;
}
public override StandardValuesCollection GetStandardValues(
ITypeDescriptorContext context)
{
return defaultRelations;
}
}
不過如何做一個更加定制化的UI呢?我們可以使用UITypeEditor類
一些類似於Image
[Editor(typeof(GradeEditor)
[TypeConverter(typeof(GradeConverter))]
public struct Grade
{
private int grade;
public Grade(int grade)
{
this
}
public int Value
{
get
{
return grade;
}
}
}
當我們輸入一個年齡的時候
圖
實現它並不困難
public class GradeEditor : UITypeEditor
{
public override bool GetPaintValueSupported(
ITypeDescriptorContext context)
{
// let the property browser know we
// to do custom painting
return true;
}
public override void PaintValue(PaintValueEventArgs pe)
{
// choose the right bitmap based on the value
string bmpName = null;
Grade g = (Grade)pe
if (g
{
bmpName =
}
else if (g
{
bmpName =
}
else
{
bmpName =
}
// draw that bitmap onto the surface provided
Bitmap b = new Bitmap(typeof(GradeEditor)
pe
b
}
}
像我們上面提到的
From:http://tw.wingwit.com/Article/program/net/201311/11959.html