@@ -120,20 +120,42 @@ public override string ToString()
120120 /// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
121121 public static bool operator != ( GridLength left , GridLength right ) => ! ( left == right ) ;
122122
123- private sealed class GridLengthTypeConverter : TypeConverter
123+ public sealed class GridLengthTypeConverter : TypeConverter
124124 {
125125 public override bool CanConvertFrom ( ITypeDescriptorContext ? context , Type ? sourceType )
126- => sourceType == typeof ( double ) ;
126+ => sourceType == typeof ( double ) || sourceType == typeof ( string ) ;
127127
128128 public override object ConvertFrom ( ITypeDescriptorContext ? context , CultureInfo ? culture , object ? value )
129129 => value switch
130130 {
131131 double d => ( GridLength ) d ,
132+ string strValue => strValue . Trim ( ) 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+ } ,
132142 _ => throw new NotSupportedException ( ) ,
133143 } ;
134144
135- public override bool CanConvertTo ( ITypeDescriptorContext ? context , Type ? destinationType ) => false ;
136- public override object ConvertTo ( ITypeDescriptorContext ? context , CultureInfo ? culture , object ? value , Type ? destinationType ) => throw new NotSupportedException ( ) ;
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+ }
137159 }
138160 }
139161}
0 commit comments