ZoomLevelCollectionConverter.cs
3.2 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace Acc.ImageBox
{
public class ZoomLevelCollectionConverter
: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string data;
ZoomLevelCollection result;
data = value as string;
if (!string.IsNullOrEmpty(data))
{
char separator;
string[] items;
TypeConverter converter;
if (culture == null)
culture = CultureInfo.CurrentCulture;
result = new ZoomLevelCollection();
separator = culture.TextInfo.ListSeparator[0];
items = data.Split(separator);
converter = TypeDescriptor.GetConverter(typeof(int));
foreach (string item in items)
result.Add((int)converter.ConvertFromString(context, culture, item));
}
else
result = null;
return result;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
object result;
if (destinationType == null)
throw new ArgumentNullException("destinationType");
if (value is ZoomLevelCollection)
{
if (destinationType == typeof(string))
{
ZoomLevelCollection collection;
StringBuilder data;
string separator;
TypeConverter converter;
collection = (ZoomLevelCollection)value;
if (culture == null)
culture = CultureInfo.CurrentCulture;
separator = culture.TextInfo.ListSeparator + " ";
converter = TypeDescriptor.GetConverter(typeof(int));
data = new StringBuilder();
foreach (int item in collection)
{
if (data.Length != 0)
data.Append(separator);
data.Append(converter.ConvertToString(context, culture, item));
}
result = data.ToString();
}
else if (destinationType == typeof(InstanceDescriptor))
{
ZoomLevelCollection collection;
ConstructorInfo constructor;
collection = (ZoomLevelCollection)value;
constructor = typeof(ZoomLevelCollection).GetConstructor(new Type[] { typeof(IList<int>) });
result = new InstanceDescriptor(constructor, new object[] { collection.ToArray() });
}
else
result = null;
}
else
result = null;
if (result == null)
result = base.ConvertTo(context, culture, value, destinationType);
return result;
}
}
}