【问题标题】:PropertyGrid handling unknown types / custom propertiesPropertyGrid 处理未知类型/自定义属性
【发布时间】:2012-11-02 13:31:47
【问题描述】:

我刚刚尝试将 PropertyGrid 用于我的“游戏编辑器”,以便更轻松地编辑对象等,但是如何处理未知类型?

例如,我使用 XNA,Texture2D 是 XNA 框架的一种类型(其他运行良好,例如 Point/Vector),但 Texture2D 只是其核心的位图,所以有没有办法“处理”未知类型和自定义 PropertyGrid 如何显示它们?

【问题讨论】:

    标签: c# properties xna propertygrid


    【解决方案1】:

    您可以使用TypeConverters

    有从 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;
            }
        }  
    }
    

    【讨论】:

    • 由于具有“默认数据类型”的类有效,因此不需要这样做。我可以轻松地添加一个带有几个 Vector3/Point(来自 XNA 框架)的类,而无需转换类型。但是对于与其他类型相似的类型:Texture2D -> Image,我想要一些例子。
    • 所以,PropertyGrid 可以处理 Vector3 和 Point 等,而无需我进行任何自定义。
    • 您在询问自定义数据类型,这是 propertygrid 处理它们的方式...如果更具体地提供您想要做什么的示例,那就太好了..
    • 好吧,我询问了 Texture2D 并专门写了具有“默认类型”的数据类型已经有效,例如 Point、Vector2、Vector3 等。包含默认类型(int、float、double、string 等)的结构。
    • 您不清楚“自定义”和“默认”类型之间的区别,Texure2D 在 xna 框架中定义为 Vector3 或 Point 。但是这些有一个 DataType 转换器。如果您创建一个我认为是“自定义”类型的类型,您应该使用 TypeConverter,但是如果您想自定义以一般方式显示的属性,您可以使用自定义 PropertyTab 和自定义 PropertyDescriptors,这里是一个示例ozcandegirmenci.com/post/2008/08/…,你会发现这是一个更硬核的解决方案……
    【解决方案2】:

    您不清楚“自定义”和“默认”类型之间的区别,Texure2D 在 xna 框架中定义为 Vector3 或 Point 。但是这些有一个 DataType 转换器。如果您创建一个我认为是“自定义”类型的类型,您应该使用 TypeConverter,但是如果您想自定义以一般方式显示的属性,您可以使用自定义 PropertyTab 和自定义 PropertyDescriptors,这里是一个示例ozcandegirmenci.com/post/2008/08/...,您会意识到这是一个更核心的解决方案... – Blau

    http://www.ozcandegirmenci.com/post/2008/08/Extending-PropertyGrid-Adding-custom-PropertyTab-and-PropertyDescritors.aspx 链接已更改。你可以到达这里。

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

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多