让Vector3属性在PropertyGrid空间中显示的Vector3Converter
2013-10-18 17:01
253 查看
当我们的自己的对象显示在PropertyGrid时,遇到标准以外的类型时,就显示不出来,例如我这里的Vector3
这时就需要自己写一个Vector3Converter了
我们的属性需要这样写
[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; }
效果如下
这时就需要自己写一个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; }
效果如下
相关文章推荐
- Extjs3 Ext.grid.PropertyGrid 消除属性显示排序
- Failed to create .NET Frameworks PropertyGrid component.(工程属性不显示 的解决方案)
- PropertyGrid控件扩展--自定义显示属性内容、属性名称及分类
- C# 设计时动态改变实体在PropertyGrid中显示出来的属性
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- 运行时自定义PropertyGrid显示属性项目
- C# 设计时动态改变实体在PropertyGrid中显示出来的属性
- 运行时自定义PropertyGrid显示属性项目
- 转 C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- 运行时自定义PropertyGrid显示属性项目
- PropertyGrid 中修改属性后, 即时显示出来
- PropertyGrid 显示自定义类属性
- [原创]使 PropertyGrid 控件显示中文属性
- 开发自己的窗体设计器(PropertyGrid显示中文属性名)
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- 优盘里的东西不显示,但查看属性却占着很大的空间怎么办
- maixmo自定义控件, maximo增加控件属性, maximo小数个格式化显示
- 显示打印机属性对话框 --API函数