프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / WrapPanel / LengthConverter.cs @ 503cb09e

이력 | 보기 | 이력해설 | 다운로드 (8.45 KB)

1 787a4489 KangIngu
// (c) Copyright Microsoft Corporation.
2
// This source is subject to the Microsoft Public License (Ms-PL).
3
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
4
// All other rights reserved.
5
6
using System;
7
using System.Collections.Generic;
8
using System.Diagnostics.CodeAnalysis;
9
using System.ComponentModel;
10
using System.Globalization;
11
using System.Windows.Controls;
12
13
namespace System.Windows
14
{
15
    /// <summary>
16
    /// Converts instances of other types to and from instances of a double that
17
    /// represent an object measurement such as a height or width.
18
    /// </summary>
19
    /// <QualityBand>Stable</QualityBand>
20
    public partial class LengthConverter : TypeConverter
21
    {
22
        /// <summary>
23
        /// Conversions from units to pixels.
24
        /// </summary>
25
        private static Dictionary<string, double> UnitToPixelConversions = new Dictionary<string, double>
26
        {
27
            { "px", 1.0 },
28
            { "in", 96.0 },
29
            { "cm", 37.795275590551178 },
30
            { "pt", 1.3333333333333333 }
31
        };
32
33
        /// <summary>
34
        /// Initializes a new instance of the
35
        /// <see cref="T:System.Windows.LengthConverter" /> class.
36
        /// </summary>
37
        public LengthConverter()
38
        {
39
        }
40
41
        /// <summary>
42
        /// Determines whether conversion is possible from a specified type to a
43
        /// <see cref="T:System.Double" /> that represents an object
44
        /// measurement.
45
        /// </summary>
46
        /// <param name="typeDescriptorContext">
47
        /// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" />
48
        /// that provides a format context.
49
        /// </param>
50
        /// <param name="sourceType">
51
        /// A <see cref="T:System.Type" /> that represents the type you want to
52
        /// convert from.
53
        /// </param>
54
        /// <returns>
55
        /// True if this converter can perform the conversion; otherwise, false.
56
        /// </returns>
57
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#", Justification = "Compat with WPF.")]
58
        public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
59
        {
60
            // Convert numeric types and strings
61
            switch (Type.GetTypeCode(sourceType))
62
            {
63
                case TypeCode.Int16:
64
                case TypeCode.UInt16:
65
                case TypeCode.Int32:
66
                case TypeCode.UInt32:
67
                case TypeCode.Int64:
68
                case TypeCode.UInt64:
69
                case TypeCode.Single:
70
                case TypeCode.Double:
71
                case TypeCode.Decimal:
72
                case TypeCode.String:
73
                    return true;
74
                default:
75
                    return false;
76
            }
77
        }
78
79
        /// <summary>
80
        /// Converts from the specified value to values of the
81
        /// <see cref="T:System.Double" /> type.
82
        /// </summary>
83
        /// <param name="typeDescriptorContext">
84
        /// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" />
85
        /// that provides a format context.
86
        /// </param>
87
        /// <param name="cultureInfo">
88
        /// The <see cref="T:System.Globalization.CultureInfo" /> to use as the
89
        /// current culture.
90
        /// </param>
91
        /// <param name="source">The value to convert.</param>
92
        /// <returns>The converted value.</returns>
93
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#", Justification = "Compat with WPF.")]
94
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "1#", Justification = "Compat with WPF.")]
95
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "2#", Justification = "Compat with WPF.")]
96
        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
97
        {
98
            if (source == null)
99
            {
100
                string message = string.Format(
101
                    CultureInfo.CurrentCulture,
102
                    "thrown when a type converter is asked to convert something it cannot.",
103
                    GetType().Name,
104
                    "null");
105
                throw new NotSupportedException(message);
106
            }
107
108
            string text = source as string;
109
            if (text != null)
110
            {
111
                // Convert Auto to NaN
112
                if (string.Compare(text, "Auto", StringComparison.OrdinalIgnoreCase) == 0)
113
                {
114
                    return double.NaN;
115
                }
116
117
                // Get the unit conversion factor
118
                string number = text;
119
                double conversionFactor = 1.0;
120
                foreach (KeyValuePair<string, double> conversion in UnitToPixelConversions)
121
                {
122
                    if (number.EndsWith(conversion.Key, StringComparison.Ordinal))
123
                    {
124
                        conversionFactor = conversion.Value;
125
                        number = text.Substring(0, number.Length - conversion.Key.Length);
126
                        break;
127
                    }
128
                }
129
130
                // Convert the value
131
                try
132
                {
133
                    return conversionFactor * Convert.ToDouble(number, cultureInfo);
134
                }
135
                catch (FormatException)
136
                {
137
                    string message = string.Format(
138
                        CultureInfo.CurrentCulture,
139
                        "thrown when a type converter fails to convert a value to another type.",
140
                        GetType().Name,
141
                        text,
142
                        typeof(double).Name);
143
                    throw new FormatException(message);
144
                }
145
            }
146
147
            return Convert.ToDouble(source, cultureInfo);
148
        }
149
150
        /// <summary>
151
        /// Returns whether the type converter can convert a measurement to the
152
        /// specified type.
153
        /// </summary>
154
        /// <param name="typeDescriptorContext">
155
        /// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" />
156
        /// that provides a format context.
157
        /// </param>
158
        /// <param name="destinationType">
159
        /// A <see cref="T:System.Type" /> that represents the type you want to
160
        /// convert to.
161
        /// </param>
162
        /// <returns>
163
        /// True if this converter can perform the conversion; otherwise, false.
164
        /// </returns>
165
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#", Justification = "Compat with WPF.")]
166
        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
167
        {
168
            return TypeConverters.CanConvertTo<double>(destinationType);
169
        }
170
171
        /// <summary>
172
        /// Converts the specified measurement to the specified type.
173
        /// </summary>
174
        /// <param name="typeDescriptorContext">
175
        /// An object that provides a format context.
176
        /// </param>
177
        /// <param name="cultureInfo">
178
        /// The <see cref="T:System.Globalization.CultureInfo" /> to use as the
179
        /// current culture.
180
        /// </param>
181
        /// <param name="value">The value to convert.</param>
182
        /// <param name="destinationType">
183
        /// A <see cref="T:System.Type" /> that represents the type you want to
184
        /// convert to.
185
        /// </param>
186
        /// <returns>The converted value.</returns>
187
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#", Justification = "Compat with WPF.")]
188
        [SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "1#", Justification = "Compat with WPF.")]
189
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
190
        {
191
            // Convert the length to a String
192
            if (value is double)
193
            {
194
                double length = (double)value;
195
                if (destinationType == typeof(string))
196
                {
197
                    return length.IsNaN() ?
198
                        "Auto" :
199
                        Convert.ToString(length, cultureInfo);
200
                }
201
            }
202
203
            return TypeConverters.ConvertTo(this, value, destinationType);
204
        }
205
    }
206
}
클립보드 이미지 추가 (최대 크기: 500 MB)