TypeConverter用于对象列表

时间:2015-06-10 15:46:49

标签: c# winforms propertygrid typeconverter

我正在尝试创建一个窗体,它使用属性网格来显示对象列表。现在我有几节课。第一个,DisplayContainer,只保存一个对象列表,它的一个实例充当我的属性网格的选定对象

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class DisplayContainer
    {
        [CategoryAttribute("XRecord Data")]
        public string Name { get; set; } //Name of the collection to uniquely ID it

        [TypeConverter(typeof(ExpandableObjectConverter)),
        CategoryAttribute("XRecord Data")]
        public List<object> Objects { get; set; } //List of the objects to be manipulated

        public DisplayContainer()
        {
            Objects = new List<object>();
        }
    }

我想要存储的对象是我的Serialine类的实例

    [TypeConverter(typeof(SerialineConverter))]
    public class Serialine
    {
        [CategoryAttribute("Points")]
        public Seriapoint FirstPoint { get; set; }
        [CategoryAttribute("Points")]
        public Seriapoint SecondPoint { get; set; }
        [CategoryAttribute("Dimensions"),
        ReadOnlyAttribute(true)]
        public double Length { get; set; }

        public Serialine()
        {

        }

        public Serialine(Seriapoint passedFirstPoint, Seriapoint passedSecondPoint)
        {
            FirstPoint = passedFirstPoint;
            SecondPoint = passedSecondPoint;
            UpdateLength();
        }

        public void UpdateLength()
        {
            double a = FirstPoint.X - SecondPoint.X;
            double b = FirstPoint.Y - SecondPoint.Y;
            double c = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));

            Length = c;
        }

        public override string ToString()
        {
            return "First Point: " + FirstPoint.ToString() + "; Second Point: " + SecondPoint.ToString() + "; Length: " + Length.ToString() + ";";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pointToParse">String representation of point in the form of (X, Y, Z)</param>
        public static Seriapoint ParsePoint(string pointToParse)
        {
            string[] coordinates = pointToParse.Split(',');
            try
            {
                double xValue = Convert.ToDouble(coordinates[0]);
                double yValue = Convert.ToDouble(coordinates[1]);
                double zValue = Convert.ToDouble(coordinates[2]);

                Seriapoint pointToReturn = new Seriapoint(xValue, yValue, zValue);
                return pointToReturn;
            }
            catch (FormatException)
            {
                return null;
            }
        }
    }

使用此转换器

    public class SerialineConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) 
        {
            if (destinationType == typeof(Serialine))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }


        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
        {
            if (destinationType == typeof(System.String) && value is Serialine)
            {
                Serialine sl = (Serialine)value; 
                return "Success: " + sl.FirstPoint.ToString() + " to " + sl.SecondPoint.ToString() + " with length " + sl.Length;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    string s = (string)value;
                    Serialine sl = new Serialine();
                    string[] parameters = s.Split(';');
                    sl.FirstPoint = Serialine.ParsePoint(parameters[0]);
                    sl.SecondPoint = Serialine.ParsePoint(parameters[1]);
                    sl.Length = Convert.ToDouble(parameters[2]);
                }
                catch
                {
                    throw new ArgumentException("Can not convert '" + (string)value + "' to type Serialine");
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

此课程为分数

[TypeConverter(typeof(ExpandableObjectConverter))]
    public class Seriapoint
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }

        public Seriapoint()
        {

        }

        public Seriapoint(double passedX, double passedY, double passedZ)
        {
            X = passedX;
            Y = passedY;
            Z = passedZ;
        }

        public override string ToString()
        {
            return "(" + Math.Round(X, 2) + ", " + Math.Round(Y, 2) + ", " + Math.Round(Z, 2) + ")";
        }
    }

现在,我得到的是:

enter image description here

但我想要的是下拉菜单来显示实际的对象,而不仅仅是列表的容量和计数。我认为我在DisplayContainer中放置List对象的ExpandableObjectConverter会这样做,但显然不是。

0 个答案:

没有答案