当我们的自己的对象显示在PropertyGrid时,遇到标准以外的类型时,就显示不出来,例如我这里的Vector3
这时就需要自己写一个Vector3Converter了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
namespace MyD3D
{
/// <summary>
/// Vector3Converter 的摘要说明。
/// </summary>
public class Vector3Converter : TypeConverter
{
// Methods
public Vector3Converter()
{
}
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)
{
if (!(value is string))
{
return base.ConvertFrom(context, culture, value);
}
string text = ((string)value).Trim();
if (text.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char ch = culture.TextInfo.ListSeparator[0];
string[] textArray = text.Split(new char[] { ch });
float[] numArray = new float[textArray.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
for (int i = 0; i < numArray.Length; i++)
{
numArray[i] = (float)converter.ConvertFromString(context, culture, textArray[i]);
}
if (numArray.Length != 3)
{
throw new ArgumentException("格式不正确!");
}
return new Vector3(numArray[0], numArray[1], numArray[2]);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is Vector3))
{
Vector3 vector3 = (Vector3)value;
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
string[] textArray = new string[3];
int num = 0;
textArray[num++] = converter.ConvertToString(context, culture, vector3.X);
textArray[num++] = converter.ConvertToString(context, culture, vector3.Y);
textArray[num++] = converter.ConvertToString(context, culture, vector3.Z);
return string.Join(separator, textArray);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is Vector3))
{
Vector3 size2 = (Vector3)value;
ConstructorInfo member = typeof(Vector3).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float) });
if (member != null)
{
return new InstanceDescriptor(member, new object[] { size2.X, size2.Y, size2.Z });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
return new Vector3((float)propertyValues["X"], (float)propertyValues["Y"], (float)propertyValues["X"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(Vector3), attributes).Sort(new string[] { "X", "Y", "Z" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}
我们的属性需要这样写
[System.ComponentModel.Category("摄像机位置"), TypeConverterAttribute(typeof(Vector3Converter))]
public Vector3 cameraPosition { get; set; }
[System.ComponentModel.Category("摄像机位置"), TypeConverterAttribute(typeof(Vector3Converter))]
public Vector3 cameraTarget { get; set; }
[System.ComponentModel.Category("摄像机位置"), TypeConverterAttribute(typeof(Vector3Converter))]
public Vector3 cameraUpVector { get; set; }
效果如下