C# 对象列表的TypeConverter
我正在尝试创建一个windows窗体,它使用属性网格来显示对象列表。现在我有几节课。第一个是DisplayContainer,它只包含一个对象列表,它的一个实例充当我的属性网格的选定对象C# 对象列表的TypeConverter,c#,winforms,propertygrid,typeconverter,C#,Winforms,Propertygrid,Typeconverter,我正在尝试创建一个windows窗体,它使用属性网格来显示对象列表。现在我有几节课。第一个是DisplayContainer,它只包含一个对象列表,它的一个实例充当我的属性网格的选定对象 [TypeConverter(typeof(ExpandableObjectConverter))] public class DisplayContainer { [CategoryAttribute("XRecord Data")] public st
[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>();
}
}
这节课的分数是多少
[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) + ")";
}
}
现在,我得到的是:
但是我想要的是下拉菜单来显示实际的对象,而不仅仅是列表中的容量和计数。我以为我放在DisplayContainer中的列表对象上的ExpandableObjectConverter可以做到这一点,但显然不行。这里有一些奇怪的事情。
DisplayContainer
的用途是什么?我猜Serialine
对象会出现在该列表中吗?如果是这样的话,它应该是列表
——道具网格对对象没有多大作用。然后,您的SerialineConverter
未指定为该类的TC。您希望能够在网格中添加/删除/定义序列化对象,还是仅编辑它们?我只希望能够在属性网格中编辑它们。显示容器的目的是显示不同类型对象的属性,但如果不可能,我可以将其特定于串行线。我只是想变得更一般一些,只是去掉(或忽略)DisplayContainer。将一个有效的Serialine
对象设置为propgrid的SelectedObject
:没有多少会像您所希望的那样工作(所有其他原因),但现在您应该能够优化您的问题,以便从何处开始我认为您需要自定义UITypeEditor。
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) + ")";
}
}