C# Can';t编辑点[]或列表<;点>;在设计时
我正在创建自定义控件,它将从点列表(或数组)中绘制形状。 我已经完成了基本的绘图功能,但现在我正努力在VisualStudio中获得设计时支持 我创建了两个属性:C# Can';t编辑点[]或列表<;点>;在设计时,c#,winforms,user-controls,windows-forms-designer,design-time,C#,Winforms,User Controls,Windows Forms Designer,Design Time,我正在创建自定义控件,它将从点列表(或数组)中绘制形状。 我已经完成了基本的绘图功能,但现在我正努力在VisualStudio中获得设计时支持 我创建了两个属性: private Point _point; public Point Point { get { return _point; } set { _point = value; } } private Point[] _points; public Point[] Points { get { return _
private Point _point;
public Point Point
{
get { return _point; }
set { _point = value; }
}
private Point[] _points;
public Point[] Points
{
get { return _points; }
set { _points = value; }
}
如下面屏幕所示,点
是可编辑的,但点
的编辑器不工作。对于我得到的每个属性,错误对象与目标类型不匹配。
如果我将Point
更改为MyPoint
(具有X,Y属性的自定义类),编辑器可以正常工作,但我不想创建不需要的额外类,因为编辑器在应该工作的时候无法工作
我的问题是:我是否可以使用数组或点列表作为公共属性,并对其提供设计时支持?如果可以添加对
PresentationCore
和WindowsBase
的引用,则可以使用
希望能有所帮助。您可以创建一个自定义的集合编辑器,派生
集合编辑器
并将类型(列表)
设置为集合类型,还可以为点
注册一个新的类型转换器属性
:
// Add reference to System.Design
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.ComponentModel.Design;
public class MyPointCollectionEditor : CollectionEditor
{
public MyPointCollectionEditor() : base(typeof(List<Point>)) { }
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
TypeDescriptor.AddAttributes(typeof(Point),
new Attribute[] { new TypeConverterAttribute() });
var result = base.EditValue(context, provider, value);
TypeDescriptor.AddAttributes(typeof(Point),
new Attribute[] { new TypeConverterAttribute(typeof(PointConverter)) });
return result;
}
}
//添加对System.Design的引用
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用System.ComponentModel.Design;
公共类MyPointCollectionEditor:CollectionEditor
{
公共MyPointCollectionEditor():基(typeof(List)){}
公共重写对象EditValue(ITypeDescriptorContext上下文,
IServiceProvider提供程序,对象值)
{
TypeDescriptor.AddAttributes(typeof(Point),
新属性[]{new TypeConverterAttribute()});
var result=base.EditValue(上下文、提供程序、值);
TypeDescriptor.AddAttributes(typeof(Point),
新属性[]{new TypeConverterAttribute(typeof(PointConverter))};
返回结果;
}
}
然后就可以将其注册为列表的编辑器了
:
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用系统、绘图、设计;
公共类MyClass:组件
{
public MyClass(){Points=new List();}
[编辑器(typeof(MyPointCollectionEditor)、typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共列表点{get;private set;}
}
问题可能是集合编辑器混淆了点
的点
@TimothyGroote感谢链接感谢您的建议,我将此问题保留一段时间。您的解决方案是一个选项,但需要两个依赖项。我希望尽可能地减少它们,所以可能会有一个选项使用点而不是点,但使用内置collections@Misiu,不用担心它是打开的,我很好奇是否还有其他方法不涉及自定义类或自定义容器。但出于好奇,您的目标是.NET framework的哪个版本PresentationCore
和WindowsBase
自3.0以来都是.NET框架的一部分,因此添加对它们的引用并不像添加对XNA
框架的引用那样添加依赖项?非常感谢。这个很好用。我对Designer.cs
文件有问题。添加点后,它们在Designer.cs
中作为this.shape1.points.Add(((System.Drawing.Point)(resources.GetObject(“shape1.points”))可见代码>是否可以更改此设置,以便在Designer中看到值而不是资源?我必须为它创建某种类型的转换器吗?关闭对话框后(在EditValue
之后),再次将转换器设置为PointConverter
。我编辑了这篇文章。太棒了!谢谢你的帮助。
// Add reference to System.Design
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.ComponentModel.Design;
public class MyPointCollectionEditor : CollectionEditor
{
public MyPointCollectionEditor() : base(typeof(List<Point>)) { }
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
TypeDescriptor.AddAttributes(typeof(Point),
new Attribute[] { new TypeConverterAttribute() });
var result = base.EditValue(context, provider, value);
TypeDescriptor.AddAttributes(typeof(Point),
new Attribute[] { new TypeConverterAttribute(typeof(PointConverter)) });
return result;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
public class MyClass : Component
{
public MyClass() { Points = new List<Point>(); }
[Editor(typeof(MyPointCollectionEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<Point> Points { get; private set; }
}