-
Notifications
You must be signed in to change notification settings - Fork 9
/
GridLengthConverter.cs
193 lines (178 loc) · 7.6 KB
/
GridLengthConverter.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.ComponentModel;
using System.Globalization;
namespace Appercode.UI
{
/// <summary>
/// Converts instances of other types to and from GridLength instances.
/// </summary>
public class GridLengthConverter : TypeConverter
{
#region TypeConverter methods implementations
/// <summary>
/// Determines whether a class can be converted from a given type to an instance of GridLength structure
/// </summary>
/// <param name="typeDescriptorContext">Describes the context information of a type.</param>
/// <param name="sourceType">The type of the source that is being evaluated for conversion.</param>
/// <returns>true if the converter can convert from the specified type to an instance of GridLength; otherwise, false.</returns>
public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
{
switch (Type.GetTypeCode(sourceType))
{
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
case TypeCode.String:
return true;
}
return false;
}
/// <summary>
/// Determines whether an instance of GridLength can be converted to a different type.
/// </summary>
/// <param name="typeDescriptorContext">Describes the context information of a type.</param>
/// <param name="destinationType">The desired type that this instance of GridLength is being evaluated for conversion.</param>
/// <returns>true if the converter can convert this instance of GridLength to the specified type; otherwise, false.</returns>
public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
{
return destinationType == typeof(string);
}
/// <summary>
/// Attempts to convert a specified object to an instance of GridLength.
/// </summary>
/// <param name="typeDescriptorContext">Describes the context information of a type.</param>
/// <param name="cultureInfo">Cultural specific information that should be respected during conversion.</param>
/// <param name="source">The object being converted.</param>
/// <returns>The instance of GridLength that is created from the converted source.</returns>
public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
{
GridUnitType auto;
if (source == null)
{
// throw base.GetConvertFromException(source);
throw new Exception(""); // TODO: Message
}
if (source is string)
{
return FromString((string)source, cultureInfo);
}
double num = Convert.ToDouble(source, cultureInfo);
if (double.IsNaN(num))
{
num = 1.0;
auto = GridUnitType.Auto;
}
else
{
auto = GridUnitType.Pixel;
}
return new GridLength(num, auto);
}
/// <summary>
/// Attempts to convert an instance of GridLength to a specified type.
/// </summary>
/// <param name="typeDescriptorContext">Describes the context information of a type.</param>
/// <param name="cultureInfo">Cultural specific information that should be respected during conversion.</param>
/// <param name="value">The instance of GridLength to convert.</param>
/// <param name="destinationType">The type that this instance of GridLength is converted to.</param>
/// <returns>he object that is created from the converted instance of GridLength.</returns>
public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((value != null) && (value is GridLength))
{
GridLength gl = (GridLength)value;
if (destinationType == typeof(string))
{
return ToString(gl, cultureInfo);
}
/*
//if (destinationType == typeof(InstanceDescriptor))
//{
// return new InstanceDescriptor(typeof(GridLength).GetConstructor(new Type[] { typeof(double), typeof(GridUnitType) }), new object[] { gl.Value, gl.GridUnitType });
//}
* */
}
// throw base.GetConvertToException(value, destinationType);
throw new Exception(""); // TODO: Message
}
#endregion // TypeConverter methods implementations
#region Static helper methods
/// <summary>
/// Parces a string to GridLength structure
/// </summary>
/// <param name="s"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
internal static GridLength FromString(string s, CultureInfo cultureInfo)
{
double num;
GridUnitType type;
if (string.IsNullOrEmpty(s))
{
throw new ArgumentException("s");
}
if (s == "auto")
{
num = 1;
type = GridUnitType.Auto;
}
else if (s[s.Length - 1] == '*')
{
s = s.Remove(s.Length - 1);
if (string.IsNullOrEmpty(s))
{
num = 1;
}
else
{
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
{
throw new ArgumentException("s");
}
}
type = GridUnitType.Star;
}
else
{
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
{
throw new ArgumentException("s");
}
type = GridUnitType.Pixel;
}
return new GridLength(num, type);
}
/// <summary>
/// Converts specified grid length structure to string
/// </summary>
/// <param name="gl"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
internal static string ToString(GridLength gl, CultureInfo cultureInfo)
{
switch (gl.GridUnitType)
{
case GridUnitType.Auto:
return "Auto";
case GridUnitType.Star:
if (Math.Abs(gl.Value - 1.0) < double.Epsilon)
{
return "*";
}
return Convert.ToString(gl.Value, cultureInfo) + "*";
}
return Convert.ToString(gl.Value, cultureInfo);
}
#endregion //Static helper methods
}
}