PropertyGrid处理未知类型 /自定义属性



我刚刚尝试使用"游戏编辑器"的属性格里德,以编辑对象,但是如何处理未知类型?

例如,我使用XNA和纹理2D是XNA框架的一种类型(其他效果很好,例如point/vector),但是纹理2D只是其核心的位图,因此,是否有一种方法来"处理"未知类型和自定义属性格里德如何显示?

您可以使用typeconverter

有来自TypeConverter并提供基本行为的通用类型转换器,最有用的是ExpandableObjectConverter之一,您可以用来扩展类实例。

    [TypeConverter( typeof( ExpandableObjectConverter ) )]
    public PhysicsObject Physics { get; private set; }

这是我的Point3结构及其自定义类型转换器的一个示例:

namespace Microsoft.Xna.Framework
{
#if WINDOWS
    public class Point3Converter: System.ComponentModel.ExpandableObjectConverter
    {
        public override bool CanConvertFrom( System.ComponentModel.ITypeDescriptorContext context, Type sourceType )
        {
            return sourceType == typeof( string );
        }
        public override object ConvertFrom( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value )
        {
            try
            {
                string[] tokens = (( string ) value).Split( ';' );
                return new Point3( int.Parse( tokens[0] ), int.Parse( tokens[1] ), int.Parse( tokens[2] ) );
            }
            catch
            {
                return context.PropertyDescriptor.GetValue( context.Instance );
            }
        }
        public override object ConvertTo( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType )
        {
            Point3 p = ( Point3 ) value;
            return p.X +";"+ p.Y+";" + p.Z;
        }
    }
    [System.ComponentModel.TypeConverter( typeof( Point3Converter ) )]
#endif
    public struct Point3
    {
        public int X,Y,Z;
        public static readonly Point3 UnitX = new Point3( 1, 0, 0 );
        public static readonly Point3 UnitY = new Point3( 0, 1, 0 );
        public static readonly Point3 UnitZ = new Point3( 0, 0, 1 );
        public Point3( int X, int Y, int Z )
        {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
        }
        public static Vector3 operator +( Point3 A, Vector3 B )
        {
            return new Vector3( A.X + B.X, A.Y + B.Y, A.Z + B.Z );
        }
        public static Point3 operator +( Point3 A, Point3 B )
        {
            return new Point3( A.X + B.X, A.Y + B.Y, A.Z + B.Z );
        }
        public static Point3 operator -( Point3 A, Point3 B )
        {
            return new Point3( A.X - B.X, A.Y - B.Y, A.Z - B.Z );
        }
        public static Point3 operator -( Point3 A )
        {
            return new Point3( -A.X, -A.Y, -A.Z );
        }

        public override string ToString( )
        {
            return X+";"+Y+";"+Z;
        }
    }  
}

您尚不清楚"自定义"one_answers"默认"类型之间的差异,texure2d在XNA框架中定义为vector3或point。但是这些具有数据类型转换器。如果创建类型,我认为是"自定义"类型,则应使用TypeConverter,但是如果您想以一般方式自定义属性显示的内容,则可以使用自定义属性tab和自定义属性destriptors,这是一个示例ozcandegirmenci.com/post/2008/08/其他

http://www.ozcandegirmenci.com/post/2008/08/externding-propertygrid-adding-custom-propertytab-and-propertydescritors.aspx链接已更改。您可以到达这里。

新链接:http://www.ozcandegirmenci.com/extdending-propertygrid-adding-custom-propertytab-and-propertytab-and-propertydescritors/

最新更新