您可以将属性添加到Point
类型的自定义控制,使编辑在控件的属性网格使用此代码:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Advanced),
TypeConverter(typeof(PointConverter))]
public Point MyPointProperty { get; set; }
如果您尝试相同的排序方法与SizeF
,你会发现对于PointF
,没有内置.NET TypeConverter
。你可以创建自己的,但我发现one here(并复制并粘贴下面的大部分代码)。
随着PointF
TypeConverter
你可以写在属性窗口中编辑PointF
类型的属性:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Advanced),
TypeConverter(typeof(PointFConverter))]
public PointF MyPointFProperty { get; set; }
下面是上面链接的文章中发现的PointF类型转换器代码:
/// <summary>
/// PointFConverter
/// </summary>
public class PointFConverter : ExpandableObjectConverter
{
/// <summary>
/// Creates a new instance of PointFConverter
/// </summary>
public PointFConverter() {
}
/// <summary>
/// Boolean, true if the source type is a string
/// </summary>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// Converts the specified string into a PointF
/// </summary>
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
if (value is string) {
try {
string s = (string)value;
string[] converterParts = s.Split(',');
float x = 0;
float y = 0;
if (converterParts.Length > 1) {
x = float.Parse(converterParts[0].Trim());
y = float.Parse(converterParts[1].Trim());
} else if (converterParts.Length == 1) {
x = float.Parse(converterParts[0].Trim());
y = 0;
} else {
x = 0F;
y = 0F;
}
return new PointF(x, y);
} catch {
throw new ArgumentException("Cannot convert [" + value.ToString() + "] to pointF");
}
}
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// Converts the PointF into a string
/// </summary>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
if (destinationType == typeof(string)) {
if (value.GetType() == typeof(PointF)) {
PointF pt = (PointF)value;
return string.Format("{0}, {1}", pt.X, pt.Y);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
完美。谢谢! – craigrs84
我按照你在这里所说的做了,但PointF特性仍然在设计器(VS 2012)上只读。 – Pedro77