使用TypeConverter从C#PropertyGrid中拉出一个对象

时间:2015-06-10 16:27:52

标签: c# winforms propertygrid typeconverter

我试图设计一个属性网格,我可以用它来编辑我的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;
            }
        }
    }

Serialine类将此类用于点

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

此TypeConverter可在属性网格上正确显示

    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 "Serialine at: " + 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);
        }
    }

现在,使用上面的代码,我的Serialine类显示在这样的属性网格中:

enter image description here

在显示和编辑Serialine类的情况下,我现在需要在更改后获取序列类的实例,以便我的程序使用最新版本

    private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            // ToDo: Serialine updatedLine = serialineShownInPropertyGridWithUpdatedValues
        }

0 个答案:

没有答案