使用TypeConverter从C#PropertyGrid中拉出对象
我正在尝试设计一个属性网格,可以用来编辑Serialine类的实例使用TypeConverter从C#PropertyGrid中拉出对象,c#,winforms,propertygrid,typeconverter,C#,Winforms,Propertygrid,Typeconverter,我正在尝试设计一个属性网格,可以用来编辑Serialine类的实例 [TypeConverter(typeof(SerialineConverter))] public class Serialine { [CategoryAttribute("Points")] public Seriapoint FirstPoint { get; set; } [CategoryAttribute("Points")]
[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;
}
}
}
这个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类上面的代码显示在属性网格中,如下所示:
随着Serialine类的显示和可编辑,我现在需要在Serialine类被更改时获取它的实例,以便我的程序使用最新的版本
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
// ToDo: Serialine updatedLine = serialineShownInPropertyGridWithUpdatedValues
}
你应该改进原来的问题,当你删除它时,我有一个半合成的答案…当你编辑SeriaPoint时,你的Serialine类没有更新。如果pgrid的selectedobject是从列表或集合中设置的,则列表项将自动更新。很抱歉,我删除了它,我不知道您正在编写答案。谢谢你的帮助,但我不太清楚你评论的第二部分是什么意思?我取消了这个问题。是否允许您继续编辑答案?在这种情况下,串行线是propertygrid的SelectedObject(或
((propertygrid)发件人)。SelectedObject)
。但是要更改的对象将是Seriapoint,而不是Serialine。你能澄清你到底想要什么吗?
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
// ToDo: Serialine updatedLine = serialineShownInPropertyGridWithUpdatedValues
}