我有一个名为Offset的属性的自定义控件,类型为PointF。
我希望能够从表单设计器编辑该属性(它当前已禁用)。
我读到我必须添加EditorAttribute,它指向从System.Drawing.Design.UITypeEditor派生的类。
看起来有很多已经从UITypeEditor派生的内置类型,例如:
System.ComponentModel.Design.BinaryEditor
System.ComponentModel.Design.CollectionEditor
System.ComponentModel.Design.DateTimeEditor
System.ComponentModel.Design.MultilineStringEditor
System.ComponentModel.Design.ObjectSelectorEditor
System.Drawing.Design.ColorEditor
System.Drawing.Design.ContentAlignmentEditor
System.Drawing.Design.CursorEditor
System.Drawing.Design.FontEditor
System.Drawing.Design.FontNameEditor
System.Drawing.Design.IconEditor
...等
我找不到用于编辑PointF或Point类型的文件。看起来这个功能应该已经存在,因为.NET一直在设计器中公开Point / PointF类型。
我希望我不必重新发明轮子 - 如果它已经存在,我想使用内置的UITypeEditor类型。
感谢。
答案 0 :(得分:3)
您可以将属性添加到Point
类型的自定义控件,该控件允许使用以下代码在控件的属性网格中进行编辑:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Advanced),
TypeConverter(typeof(PointConverter))]
public Point MyPointProperty { get; set; }
如果您使用SizeF
尝试相同的方法,您会发现TypeConverter
没有内置的.NET PointF
。你可以创建自己的,我发现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);
}
}