33using System . ComponentModel ;
44using System . Diagnostics ;
55using System . Globalization ;
6+ using Microsoft . Maui . Converters ;
67
78namespace Microsoft . Maui
89{
@@ -119,43 +120,5 @@ public override string ToString()
119120 /// <param name="right">The second GridLength to compare.</param>
120121 /// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
121122 public static bool operator != ( GridLength left , GridLength right ) => ! ( left == right ) ;
122-
123- public sealed class GridLengthTypeConverter : TypeConverter
124- {
125- public override bool CanConvertFrom ( ITypeDescriptorContext ? context , Type ? sourceType )
126- => sourceType == typeof ( double ) || sourceType == typeof ( string ) ;
127-
128- public override object ConvertFrom ( ITypeDescriptorContext ? context , CultureInfo ? culture , object ? value )
129- => value switch
130- {
131- double d => ( GridLength ) d ,
132- string strValue => strValue . Trim ( ) . ToLowerInvariant ( ) switch
133- {
134- "auto" => GridLength . Auto ,
135- "*" => new GridLength ( 1 , GridUnitType . Star ) ,
136- #pragma warning disable CA1846 , CA1865
137- _ when strValue . EndsWith ( "*" , StringComparison . Ordinal ) && double . TryParse ( strValue . Substring ( 0 , strValue . Length - 1 ) , NumberStyles . Number , CultureInfo . InvariantCulture , out var length ) => new GridLength ( length , GridUnitType . Star ) ,
138- #pragma warning restore CA1846 , CA1865
139- _ when double . TryParse ( strValue , NumberStyles . Number , CultureInfo . InvariantCulture , out var length ) => new GridLength ( length ) ,
140- _ => throw new FormatException ( ) ,
141- } ,
142- _ => throw new NotSupportedException ( ) ,
143- } ;
144-
145- public override bool CanConvertTo ( ITypeDescriptorContext ? context , Type ? destinationType ) => destinationType == typeof ( string ) ;
146- public override object ConvertTo ( ITypeDescriptorContext ? context , CultureInfo ? culture , object ? value , Type ? destinationType )
147- {
148- if ( destinationType == typeof ( string ) && value is GridLength length )
149- {
150- if ( length . IsAuto )
151- return "auto" ;
152- if ( length . IsStar )
153- return $ "{ length . Value . ToString ( CultureInfo . InvariantCulture ) } *";
154- return $ "{ length . Value . ToString ( CultureInfo . InvariantCulture ) } ";
155- }
156- throw new NotSupportedException ( $ "Cannot convert { value ? . GetType ( ) } to { destinationType } ") ;
157-
158- }
159- }
160123 }
161124}
0 commit comments