无法编辑 Point[] 或 List<Point>在设计时

Can#39;t edit Point[] or Listlt;Pointgt; at design time(无法编辑 Point[] 或 Listlt;Pointgt;在设计时)

本文介绍了无法编辑 Point[] 或 List<Point>在设计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义控件,该控件将从点列表(或数组)中绘制形状.我已经完成了基本的绘图功能,但现在我在 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&lt;Point&gt;在设计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法编辑 Point[] 或 List&lt;Point&gt;在设计时

基础教程推荐