TypeConverter for a List of Objects

2.3k views Asked by At

I'm trying to create a windows form which uses a property grid to display a list of objects. Right now I have a few classes. The first one, DisplayContainer, simply holds a list of objects and an instance of it acts as the selected object for my property grid

    [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>();
        }
    }

The object I'm wanting to store is an instance of my Serialine class

    [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;
            }
        }
    }

which uses this converter

    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);
        }
    }

And this class for points

[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) + ")";
        }
    }

Now, what I'm getting is this:

enter image description here

But what I want is for dropping down the menu to show the actual objects instead of just a capacity and count for the list. I thought the ExpandableObjectConverter I put over my List objects in DisplayContainer would do that but apparently its not.

0

There are 0 answers