Autor Beitrag
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 10.01.06 17:11 
Hallo!

Ich arbeite momentan an einer Komponente, welche Datenpunkte graphisch darstellen soll. Bisher funktioniert das auch alles ganz wunderbar, doch nun bin ich auf ein Problem gestoßen. Ich möchte der Komponente eine Property vom Typ PointF geben, in der ein Datenpunkt angegeben werden kann.

Ich hatte gehofft, dass man diese dann im Property Window genauso setzen kann wie eine Property vom Typ Point. Leider ist die Property von Typ PointF im Property Window read-only. Wie kann ich das ändern?

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Christian S. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 10.01.06 18:46 
Okay, mit PointF scheint das nicht zu gehen, weil es keinen TypeConverter dafür gibt. Ich arbeite anstatt mit PointF nun mit der ohnehin intern verwendeten eigenen Klasse DataPoint, welche wie PointF die Properties X und Y besitzt, welche aber vom Typ Double sind.

Dafür habe ich nun einen eigenen TypeConverter geschrieben, wie es hier beschrieben ist:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
    public class DataPointConverter : ExpandableObjectConverter
    {
        public override bool  CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(DataPoint))
                return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object  ConvertTo(ITypeDescriptorContext context,
            System.Globalization.CultureInfo culture,
            object value,
            Type destinationType)
        {
            if (destinationType == typeof(System.String) &&
                value is DataPoint)
            {
                return ((DataPoint)value).X.ToString()+"; "+((DataPoint)value).Y.ToString();
            }


           return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool  CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(System.String))
                return true;

           return base.CanConvertFrom(context, sourceType);
        }

        public override object  ConvertFrom(ITypeDescriptorContext context,
            System.Globalization.CultureInfo culture,
            object value)
        {
            if (value is string)
            {
                try{
                    string s = (string)value;
                    string[] parts = s.Split(new char[] {';'});
                    
                    DataPoint dp = new DataPoint();
                    dp.X = Double.Parse(parts[0].Trim());
                    dp.Y = Double.Parse(parts[1].Trim());
                    return dp;
                }
                catch
                {
                    throw new ArgumentException("Cannot convert!");
                }
            }
           return base.ConvertFrom(context, culture, value);
        }
    }


Damit ist das Problem bis auf ein paar Kleinigkeiten gelöst, aber hinter die komme ich noch selber ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".