8

タイプのプロパティを持つカスタムコントロールがありますCollection<System.Drawing.Point>CollectionEditorこのプロパティを編集するために使用すると、CollectionEditorウィンドウにとプロパティが表示"Object does not match target type."されます。しかし、代わりに使用すれば、失敗はありません。"X""Y"System.Drawing.PointF

この違いが発生する理由を誰かが説明できますか?

4

3 に答える 3

3

PointとPointFの違いは、確かにPointConverterにあります。これが問題を引き起こす理由はかなり長い話ですが、結局のところ、次のように要約されます。

System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)実装はSystem.ComponentModel.Design.CollectionEditor .CollectionEditorCollectionForm.SelectionWrapper単にを返しますthis

インターフェイスの前述のメソッドのMSDNページによるとICustomTypeDescriptor、実装は

指定されたプロパティ記述子によって記述されたプロパティを含むオブジェクトを返します。

私がそれを正しく理解していれば、この場合、実装はドキュメントと矛盾します。

これは私自身の研究に基づいているので、当然のこととは思わないでください。この問題のレポートをMicrosoftConnectに投稿したので、数日以内に確実にわかることを願っています。回答がありましたらご報告いたします。

于 2010-04-19T10:42:57.227 に答える
2

私は.NET/C#の専門家ではありませんが、問題はクラス内のどこかにあるようです。これはクラスのforPointConverterとして使用されます。コレクションエディタは、クラス内で失敗するものを使用している必要があります。TypeConverterAttributeSystem.Drawing.PointPointConverter

クラスにがないPointConverterので、問題なく動作しているのではないかと思います。PointFTypeConverterAttribute

次の例では、 MSDNのコードを使用Pointしてまとめましたが、コレクション内のクラスを使用すると問題が発生しますがMyPoint、カスタムを使用しているクラスでは問題が発生しませんTypeConverter

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace WindowsControlLibrary1
{
    public class MyTypeConverter : TypeConverter
    {
        // Overrides the CanConvertFrom method of TypeConverter.
        // The ITypeDescriptorContext interface provides the context for the
        // conversion. Typically, this interface is used at design time to 
        // provide information about the design-time container.
        public override bool CanConvertFrom(ITypeDescriptorContext context,
           Type sourceType)
        {

            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        // Overrides the ConvertFrom method of TypeConverter.
        public override object ConvertFrom(ITypeDescriptorContext context,
           CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] v = ((string)value).Split(new char[] { ',' });
                return new MyPoint(int.Parse(v[0]), int.Parse(v[1]));
            }
            return base.ConvertFrom(context, culture, value);
        }
        // Overrides the ConvertTo method of TypeConverter.
        public override object ConvertTo(ITypeDescriptorContext context,
           CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return ((MyPoint)value).X + "," + ((MyPoint)value).Y;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    [SerializableAttribute]
    [TypeConverterAttribute(typeof(MyTypeConverter))]
    public struct MyPoint
    {
        private int x;
        private int y;

        public MyPoint(int _x, int _y)
        {
            x = _x;
            y = _y;
        }

        public int X
        {
            get { return x; }
            set { x = value; }
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }

    }

    public partial class UserControl1 : UserControl
    {
        private List<System.Drawing.Point> points;
        private List<System.Drawing.PointF> pointfs;
        private List<MyPoint> mypoints;


        public List<System.Drawing.Point> PointList
        {
            get{ return points;}
            set{ points = value;}
        }

        public List<System.Drawing.PointF> PointFList
        {
            get {return pointfs;}
            set{pointfs = value;}
        }

        public List<MyPoint> MyPointList
        {
            get { return mypoints; }
            set { mypoints = value; }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}
于 2010-04-16T21:31:38.953 に答える
0

私の解決策は、collectioneditorを使用して(ポイントの)リストを編集し、を使用 TypeDescriptor.AddAttributes(GetType(Drawing.Point), New TypeConverterAttribute())してPointのtypeconverterを何も設定せず、その後を使用TypeDescriptor.AddAttributes(GetType(Drawing.Point), New TypeConverterAttribute(GetType(PointConverter)))してtypeconverterをデフォルトに設定することです。

于 2017-06-23T14:54:55.707 に答える