WINFORM.PropertyGrid学习(一)中只列出了简单的效果,且控件中只有简单的属性。如果控件中需要显示像Size、Font、Color等复杂的自定义属性该如何实现呢?此时需要一个转换器,将自定义的类型,转换成PropertyGrid可以显示的类型。
///自定义转换器类,派生于TypeConverter
public class MyPointConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string text = value as string;
if (text == null)
{
return base.ConvertFrom(context, culture, value);
}
string text2 = text.Trim();
if (text2.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char separator = culture.TextInfo.ListSeparator[0];
string[] array = text2.Split(new char[] { separator }, StringSplitOptions.None);
int[] array2 = new int[array.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
for (int i = 0; i < array2.Length; i++)
{
array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
}
if (array2.Length == 2)
{
return new MyPonit(array2[0], array2[1]);
}
throw new ArgumentException("");
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is MyPonit)
{
MyPonit point = (MyPonit)value;
if (destinationType == typeof(string))
{
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string[] value2 = new string[]
{
converter.ConvertToString(context, culture, point.XX),
converter.ConvertToString(context, culture, point.Y)
};
return string.Join(separator, value2);
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo constructor = typeof(MyPonit).GetConstructor(new Type[]
{
typeof(int),
typeof(int)
});
if (constructor != null)
{
return new InstanceDescriptor(constructor, new object[]
{
point.XX,
point.Y
});
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
object obj = propertyValues["XX"];
object obj2 = propertyValues["Y"];
if (obj == null || obj2 == null || !(obj is int) || !(obj2 is int))
{
throw new ArgumentException("");
}
return new MyPonit((int)obj, (int)obj2);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(MyPonit), attributes);
return properties.Sort(MyPointConverter.s_propertySort);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
private static readonly string[] s_propertySort = new string[] { "XX", "Y" };
}
[TypeConverter(typeof(MyPointConverter))]//转换器用于复杂类型
public class MyPonit//自定义的复杂类型,需要在空间中显示
{
int x;
int y;
public int XX
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public MyPonit(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class AppSetting
{
MyPonit myPonit;
public MyPonit MyPonit
{
get
{
if (myPonit == null)
{
return new MyPonit(0, 0);
}
else
{
return myPonit;
}
}
set
{
myPonit = value;
}
}
}
//绑定控件
AppSetting appSetting = new AppSetting();
private void Form1_Load(object sender, EventArgs e)
{
this.propertyGrid1.SelectedObject = appSetting;
}