Can#39;t edit Point[] or Listlt;Pointgt; at design time(无法编辑 Point[] 或 Listlt;Pointgt;在设计时)
问题描述
我正在创建自定义控件,该控件将从点列表(或数组)中绘制形状.我已经完成了基本的绘图功能,但现在我在 Visual Studio 中的设计时支持上苦苦挣扎.
I'm creating custom control that will draw shape from list (or array) of points. I have basic drawing functionality done, but now I'm struggling with design-time support in Visual Studio.
我创建了两个属性:
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
是可编辑的,但 Points
的编辑器不起作用.对于每个属性,我得到错误 Object does not match target type.
As seen on screen below Point
is editable, but editor for Points
isn't working. For each property I get error Object does not match target type.
如果我将 Point
更改为 MyPoint
(具有 X、Y 属性的自定义类)编辑器工作得很好,但我不想创建不需要的额外类,因为编辑器在应该的时候不起作用.
If I change Point
to MyPoint
(custom class with X,Y properties) editor works just fine, but I don't want to create unneeded extra class because editor does not work when it should.
我的问题是:我可以使用数组或点列表作为公共属性并为其提供设计时支持吗?
My question is: Can I use array or list of point as public property and have design-time support for it?
推荐答案
您可以创建一个自定义集合编辑器,派生 CollectionEditor
并将 typeof(List
设置为集合类型,同时为Point
注册一个新的TypeConverterAttribute
:
You can create a custom collection editor deriving CollectionEditor
and set typeof(List<Point>)
as collection type, also register a new TypeConverterAttribute
for Point
:
// 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;
}
}
然后将其注册为 List<Point>
的编辑器就足够了:
Then it's enough to register it as editor of your List<Point>
:
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; }
}
这篇关于无法编辑 Point[] 或 List<Point>在设计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法编辑 Point[] 或 List<Point>在设计时
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01